home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb.new / gdb-3.98 / gdb / symtab.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-01  |  79.6 KB  |  2,953 lines

  1. /* Symbol table lookup for the GNU debugger, GDB.
  2.    Copyright (C) 1986, 1987, 1988, 1989, 1990 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include <stdio.h>
  21. #include "defs.h"
  22. #include "symtab.h"
  23. #include "param.h"
  24. #include "gdbcore.h"
  25. #include "frame.h"
  26. #include "target.h"
  27. #include "value.h"
  28. #include "symfile.h"
  29. #include "gdbcmd.h"
  30. #include "regex.h"
  31.  
  32. #include <obstack.h>
  33. #include <assert.h>
  34.  
  35. #include <sys/types.h>
  36. #include <fcntl.h>
  37. #include <string.h>
  38. #include <sys/stat.h>
  39.  
  40. extern char *getenv ();
  41.  
  42. extern char *cplus_demangle ();
  43. extern struct value *value_of_this ();
  44. extern void break_command ();
  45. extern void select_source_symtab ();
  46.  
  47. /* Functions this file defines */
  48. static int find_line_common ();
  49. struct partial_symtab *lookup_partial_symtab ();
  50. static struct partial_symbol *lookup_partial_symbol ();
  51. static struct partial_symbol *lookup_demangled_partial_symbol ();
  52. static struct symbol *lookup_demangled_block_symbol ();
  53.  
  54. /* These variables point to the objects
  55.    representing the predefined C data types.  */
  56.  
  57. struct type *builtin_type_void;
  58. struct type *builtin_type_char;
  59. struct type *builtin_type_short;
  60. struct type *builtin_type_int;
  61. struct type *builtin_type_long;
  62. struct type *builtin_type_long_long;
  63. struct type *builtin_type_unsigned_char;
  64. struct type *builtin_type_unsigned_short;
  65. struct type *builtin_type_unsigned_int;
  66. struct type *builtin_type_unsigned_long;
  67. struct type *builtin_type_unsigned_long_long;
  68. struct type *builtin_type_float;
  69. struct type *builtin_type_double;
  70. struct type *builtin_type_error;
  71.  
  72. /* Block in which the most recently searched-for symbol was found.
  73.    Might be better to make this a parameter to lookup_symbol and 
  74.    value_of_this. */
  75. struct block *block_found;
  76.  
  77. char no_symtab_msg[] = "No symbol table is loaded.  Use the \"file\" command.";
  78.  
  79. /* Check for a symtab of a specific name; first in symtabs, then in
  80.    psymtabs.  *If* there is no '/' in the name, a match after a '/'
  81.    in the symtab filename will also work.  */
  82.  
  83. static struct symtab *
  84. lookup_symtab_1 (name)
  85.      char *name;
  86. {
  87.   register struct symtab *s;
  88.   register struct partial_symtab *ps;
  89.   register char *slash = strchr (name, '/');
  90.   register int len = strlen (name);
  91.  
  92.   for (s = symtab_list; s; s = s->next)
  93.     if (!strcmp (name, s->filename))
  94.       return s;
  95.  
  96.   for (ps = partial_symtab_list; ps; ps = ps->next)
  97.     if (!strcmp (name, ps->filename))
  98.       {
  99.     if (ps->readin)
  100.       fatal ("Internal: readin pst found when no symtab found.");
  101.     return PSYMTAB_TO_SYMTAB (ps);
  102.       }
  103.  
  104.   if (!slash)
  105.     {
  106.       for (s = symtab_list; s; s = s->next)
  107.     {
  108.       int l = strlen (s->filename);
  109.  
  110.       if (s->filename[l - len -1] == '/'
  111.           && !strcmp (s->filename + l - len, name))
  112.         return s;
  113.     }
  114.  
  115.       for (ps = partial_symtab_list; ps; ps = ps->next)
  116.     {
  117.       int l = strlen (ps->filename);
  118.  
  119.       if (ps->filename[l - len - 1] == '/'
  120.           && !strcmp (ps->filename + l - len, name))
  121.         {
  122.           if (ps->readin)
  123.         fatal ("Internal: readin pst found when no symtab found.");
  124.           return PSYMTAB_TO_SYMTAB (ps);
  125.         }
  126.     }
  127.     }
  128.   return 0;
  129. }
  130.  
  131. /* Lookup the symbol table of a source file named NAME.  Try a couple
  132.    of variations if the first lookup doesn't work.  */
  133.  
  134. struct symtab *
  135. lookup_symtab (name)
  136.      char *name;
  137. {
  138.   register struct symtab *s;
  139.   register char *copy;
  140.  
  141.   s = lookup_symtab_1 (name);
  142.   if (s) return s;
  143.  
  144.   /* If name not found as specified, see if adding ".c" helps.  */
  145.  
  146.   copy = (char *) alloca (strlen (name) + 3);
  147.   strcpy (copy, name);
  148.   strcat (copy, ".c");
  149.   s = lookup_symtab_1 (copy);
  150.   if (s) return s;
  151.  
  152.   /* We didn't find anything; die.  */
  153.   return 0;
  154. }
  155.  
  156. /* Lookup the partial symbol table of a source file named NAME.  This
  157.    only returns true on an exact match (ie. this semantics are
  158.    different from lookup_symtab.  */
  159.  
  160. struct partial_symtab *
  161. lookup_partial_symtab (name)
  162. char *name;
  163. {
  164.   register struct partial_symtab *s;
  165.   
  166.   for (s = partial_symtab_list; s; s = s->next)
  167.     if (!strcmp (name, s->filename))
  168.       return s;
  169.   
  170.   return 0;
  171. }
  172.  
  173. /* Return a typename for a struct/union/enum type
  174.    without the tag qualifier.  If the type has a NULL name,
  175.    NULL is returned.  */
  176. char *
  177. type_name_no_tag (type)
  178.      register struct type *type;
  179. {
  180.   register char *name = TYPE_NAME (type);
  181.   char *strchr ();
  182.   if (name == 0)
  183.     return 0;
  184.  
  185. #if 0
  186.   switch (TYPE_CODE (type))
  187.     {
  188.     case TYPE_CODE_STRUCT:
  189.       return name + 7;
  190.     case TYPE_CODE_UNION:
  191.       return name + 6;
  192.     case TYPE_CODE_ENUM:
  193.       return name + 5;
  194.     }
  195. #endif
  196.  
  197.   name = strchr (name, ' ');
  198.   if (name)
  199.     return name + 1;
  200.  
  201.   return TYPE_NAME (type);
  202. }
  203.  
  204. /* Added by Bryan Boreham, Kewill, Sun Sep 17 18:07:17 1989.
  205.  
  206.    If this is a stubbed struct (i.e. declared as struct foo *), see if
  207.    we can find a full definition in some other file. If so, copy this
  208.    definition, so we can use it in future.  If not, set a flag so we 
  209.    don't waste too much time in future.
  210.  
  211.    This used to be coded as a macro, but I don't think it is called 
  212.    often enough to merit such treatment.
  213. */
  214.  
  215. struct complaint stub_noname_complaint =
  216.   {"stub type has NULL name", 0, 0};
  217.  
  218. void 
  219. check_stub_type(type)
  220.      struct type *type;
  221. {
  222.   if (TYPE_FLAGS(type) & TYPE_FLAG_STUB)
  223.     {
  224.       char* name= type_name_no_tag (type);
  225.       struct symbol *sym;
  226.       if (name == 0)
  227.     {
  228.       complain (&stub_noname_complaint, 0);
  229.       return;
  230.     }
  231.       sym = lookup_symbol (name, 0, STRUCT_NAMESPACE, 0, 
  232.                (struct symtab **)NULL);
  233.       if (sym)
  234.     bcopy (SYMBOL_TYPE(sym), type, sizeof (struct type));
  235.     }
  236. }
  237.  
  238. /* Demangle a GDB method stub type.  */
  239. char *
  240. gdb_mangle_typename (type)
  241.      struct type *type;
  242. {
  243.   static struct type *last_type;
  244.   static char *mangled_typename;
  245.  
  246.   if (type != last_type)
  247.     {
  248.       /* Need a new type prefix.  */
  249.       char *strchr ();
  250.       char *newname = type_name_no_tag (type);
  251.       char buf[20];
  252.       int len;
  253.  
  254.       if (mangled_typename)
  255.     free (mangled_typename);
  256.  
  257.       len = strlen (newname);
  258.       sprintf (buf, "__%d", len);
  259.       mangled_typename = (char *)xmalloc (strlen (buf) + len + 1);
  260.       strcpy (mangled_typename, buf);
  261.       strcat (mangled_typename, newname);
  262.       /* Now we have built "__#newname".  */
  263.     }
  264.   return mangled_typename;
  265. }
  266.  
  267. /* Lookup a primitive type named NAME. 
  268.    Return zero if NAME is not a primitive type.*/
  269.  
  270. struct type *
  271. lookup_primitive_typename (name)
  272.      char *name;
  273. {
  274.   if (!strcmp (name, "int"))
  275.     return builtin_type_int;
  276.   if (!strcmp (name, "long"))
  277.     return builtin_type_long;
  278.   if (!strcmp (name, "short"))
  279.     return builtin_type_short;
  280.   if (!strcmp (name, "char"))
  281.     return builtin_type_char;
  282.   if (!strcmp (name, "float"))
  283.     return builtin_type_float;
  284.   if (!strcmp (name, "double"))
  285.     return builtin_type_double;
  286.   if (!strcmp (name, "void"))
  287.     return builtin_type_void;
  288.   return 0;
  289. }
  290.  
  291. /* Lookup a typedef or primitive type named NAME,
  292.    visible in lexical block BLOCK.
  293.    If NOERR is nonzero, return zero if NAME is not suitably defined.  */
  294.  
  295. struct type *
  296. lookup_typename (name, block, noerr)
  297.      char *name;
  298.      struct block *block;
  299.      int noerr;
  300. {
  301.   register struct symbol *sym =
  302.     lookup_symbol (name, block, VAR_NAMESPACE, 0, (struct symtab **)NULL);
  303.   if (sym == 0 || SYMBOL_CLASS (sym) != LOC_TYPEDEF)
  304.     {
  305.       struct type *tmp;
  306.       tmp = lookup_primitive_typename (name);
  307.       if (!tmp && noerr)
  308.     return 0;
  309.       error ("No type named %s.", name);
  310.     }
  311.   return SYMBOL_TYPE (sym);
  312. }
  313.  
  314. struct type *
  315. lookup_unsigned_typename (name)
  316.      char *name;
  317. {
  318.   if (!strcmp (name, "int"))
  319.     return builtin_type_unsigned_int;
  320.   if (!strcmp (name, "long"))
  321.     return builtin_type_unsigned_long;
  322.   if (!strcmp (name, "short"))
  323.     return builtin_type_unsigned_short;
  324.   if (!strcmp (name, "char"))
  325.     return builtin_type_unsigned_char;
  326.   error ("No type named unsigned %s.", name);
  327.   return (struct type *)-1;  /* for lint */
  328. }
  329.  
  330. /* Lookup a structure type named "struct NAME",
  331.    visible in lexical block BLOCK.  */
  332.  
  333. struct type *
  334. lookup_struct (name, block)
  335.      char *name;
  336.      struct block *block;
  337. {
  338.   register struct symbol *sym 
  339.     = lookup_symbol (name, block, STRUCT_NAMESPACE, 0, (struct symtab **)NULL);
  340.  
  341.   if (sym == 0)
  342.     error ("No struct type named %s.", name);
  343.   if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
  344.     error ("This context has class, union or enum %s, not a struct.", name);
  345.   return SYMBOL_TYPE (sym);
  346. }
  347.  
  348. /* Lookup a union type named "union NAME",
  349.    visible in lexical block BLOCK.  */
  350.  
  351. struct type *
  352. lookup_union (name, block)
  353.      char *name;
  354.      struct block *block;
  355. {
  356.   register struct symbol *sym 
  357.     = lookup_symbol (name, block, STRUCT_NAMESPACE, 0, (struct symtab **)NULL);
  358.  
  359.   if (sym == 0)
  360.     error ("No union type named %s.", name);
  361.   if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_UNION)
  362.     error ("This context has class, struct or enum %s, not a union.", name);
  363.   return SYMBOL_TYPE (sym);
  364. }
  365.  
  366. /* Lookup an enum type named "enum NAME",
  367.    visible in lexical block BLOCK.  */
  368.  
  369. struct type *
  370. lookup_enum (name, block)
  371.      char *name;
  372.      struct block *block;
  373. {
  374.   register struct symbol *sym 
  375.     = lookup_symbol (name, block, STRUCT_NAMESPACE, 0, (struct symtab **)NULL);
  376.   if (sym == 0)
  377.     error ("No enum type named %s.", name);
  378.   if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_ENUM)
  379.     error ("This context has class, struct or union %s, not an enum.", name);
  380.   return SYMBOL_TYPE (sym);
  381. }
  382.  
  383. /* Given a type TYPE, lookup the type of the component of type named
  384.    NAME.  
  385.    If NOERR is nonzero, return zero if NAME is not suitably defined.  */
  386.  
  387. struct type *
  388. lookup_struct_elt_type (type, name, noerr)
  389.      struct type *type;
  390.      char *name;
  391.      int noerr;
  392. {
  393.   int i;
  394.  
  395.   if (TYPE_CODE (type) != TYPE_CODE_STRUCT
  396.       && TYPE_CODE (type) != TYPE_CODE_UNION)
  397.     {
  398.       target_terminal_ours ();
  399.       fflush (stdout);
  400.       fprintf (stderr, "Type ");
  401.       type_print (type, "", stderr, -1);
  402.       error (" is not a structure or union type.");
  403.     }
  404.  
  405.   check_stub_type (type);
  406.  
  407.   for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
  408.     {
  409.       char *t_field_name = TYPE_FIELD_NAME (type, i);
  410.  
  411.       if (t_field_name && !strcmp (t_field_name, name))
  412.     return TYPE_FIELD_TYPE (type, i);
  413.     }
  414.   /* OK, it's not in this class.  Recursively check the baseclasses.  */
  415.   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
  416.     {
  417.       struct type *t = lookup_struct_elt_type (TYPE_BASECLASS (type, i),
  418.                            name, 0);
  419.       if (t != NULL)
  420.     return t;
  421.     }
  422.  
  423.   if (noerr)
  424.     return NULL;
  425.   
  426.   target_terminal_ours ();
  427.   fflush (stdout);
  428.   fprintf (stderr, "Type ");
  429.   type_print (type, "", stderr, -1);
  430.   fprintf (stderr, " has no component named ");
  431.   fputs_filtered (name, stderr);
  432.   error (".");
  433.   return (struct type *)-1;    /* For lint */
  434. }
  435.  
  436. /* Given a type TYPE, return a type of pointers to that type.
  437.    May need to construct such a type if this is the first use.
  438.  
  439.    C++: use TYPE_MAIN_VARIANT and TYPE_CHAIN to keep pointer
  440.    to member types under control.  */
  441.  
  442. struct type *
  443. lookup_pointer_type (type)
  444.      struct type *type;
  445. {
  446.   register struct type *ptype = TYPE_POINTER_TYPE (type);
  447.   if (ptype) return TYPE_MAIN_VARIANT (ptype);
  448.  
  449.   /* This is the first time anyone wanted a pointer to a TYPE.  */
  450.   if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
  451.     ptype  = (struct type *) xmalloc (sizeof (struct type));
  452.   else
  453.     ptype  = (struct type *) obstack_alloc (symbol_obstack,
  454.                         sizeof (struct type));
  455.  
  456.   bzero (ptype, sizeof (struct type));
  457.   TYPE_MAIN_VARIANT (ptype) = ptype;
  458.   TYPE_TARGET_TYPE (ptype) = type;
  459.   TYPE_POINTER_TYPE (type) = ptype;
  460.   /* New type is permanent if type pointed to is permanent.  */
  461.   if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
  462.     TYPE_FLAGS (ptype) |= TYPE_FLAG_PERM;
  463.   /* We assume the machine has only one representation for pointers!  */
  464.   TYPE_LENGTH (ptype) = sizeof (char *);
  465.   TYPE_CODE (ptype) = TYPE_CODE_PTR;
  466.   return ptype;
  467. }
  468.  
  469. struct type *
  470. lookup_reference_type (type)
  471.      struct type *type;
  472. {
  473.   register struct type *rtype = TYPE_REFERENCE_TYPE (type);
  474.   if (rtype) return TYPE_MAIN_VARIANT (rtype);
  475.  
  476.   /* This is the first time anyone wanted a pointer to a TYPE.  */
  477.   if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
  478.     rtype  = (struct type *) xmalloc (sizeof (struct type));
  479.   else
  480.     rtype  = (struct type *) obstack_alloc (symbol_obstack,
  481.                         sizeof (struct type));
  482.  
  483.   bzero (rtype, sizeof (struct type));
  484.   TYPE_MAIN_VARIANT (rtype) = rtype;
  485.   TYPE_TARGET_TYPE (rtype) = type;
  486.   TYPE_REFERENCE_TYPE (type) = rtype;
  487.   /* New type is permanent if type pointed to is permanent.  */
  488.   if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
  489.     TYPE_FLAGS (rtype) |= TYPE_FLAG_PERM;
  490.   /* We assume the machine has only one representation for pointers!  */
  491.   TYPE_LENGTH (rtype) = sizeof (char *);
  492.   TYPE_CODE (rtype) = TYPE_CODE_REF;
  493.   return rtype;
  494. }
  495.  
  496.  
  497. /* Implement direct support for MEMBER_TYPE in GNU C++.
  498.    May need to construct such a type if this is the first use.
  499.    The TYPE is the type of the member.  The DOMAIN is the type
  500.    of the aggregate that the member belongs to.  */
  501.  
  502. struct type *
  503. lookup_member_type (type, domain)
  504.      struct type *type, *domain;
  505. {
  506.   register struct type *mtype = TYPE_MAIN_VARIANT (type);
  507.   struct type *main_type;
  508.  
  509.   main_type = mtype;
  510.   while (mtype)
  511.     {
  512.       if (TYPE_DOMAIN_TYPE (mtype) == domain)
  513.     return mtype;
  514.       mtype = TYPE_NEXT_VARIANT (mtype);
  515.     }
  516.  
  517.   /* This is the first time anyone wanted this member type.  */
  518.   if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
  519.     mtype  = (struct type *) xmalloc (sizeof (struct type));
  520.   else
  521.     mtype  = (struct type *) obstack_alloc (symbol_obstack,
  522.                         sizeof (struct type));
  523.  
  524.   bzero (mtype, sizeof (struct type));
  525.   if (main_type == 0)
  526.     main_type = mtype;
  527.   else
  528.     {
  529.       TYPE_NEXT_VARIANT (mtype) = TYPE_NEXT_VARIANT (main_type);
  530.       TYPE_NEXT_VARIANT (main_type) = mtype;
  531.     }
  532.   TYPE_MAIN_VARIANT (mtype) = main_type;
  533.   TYPE_TARGET_TYPE (mtype) = type;
  534.   TYPE_DOMAIN_TYPE (mtype) = domain;
  535.   /* New type is permanent if type pointed to is permanent.  */
  536.   if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
  537.     TYPE_FLAGS (mtype) |= TYPE_FLAG_PERM;
  538.  
  539.   /* In practice, this is never used.  */
  540.   TYPE_LENGTH (mtype) = 1;
  541.   TYPE_CODE (mtype) = TYPE_CODE_MEMBER;
  542.  
  543. #if 0
  544.   /* Now splice in the new member pointer type.  */
  545.   if (main_type)
  546.     {
  547.       /* This type was not "smashed".  */
  548.       TYPE_CHAIN (mtype) = TYPE_CHAIN (main_type);
  549.       TYPE_CHAIN (main_type) = mtype;
  550.     }
  551. #endif
  552.  
  553.   return mtype;
  554. }
  555.  
  556. /* Allocate a stub method whose return type is
  557.    TYPE.  We will fill in arguments later.  This always
  558.    returns a fresh type.  If we unify this type with
  559.    an existing type later, the storage allocated
  560.    here can be freed.  */
  561. struct type *
  562. allocate_stub_method (type)
  563.      struct type *type;
  564. {
  565.   struct type *mtype = (struct type *)xmalloc (sizeof (struct type));
  566.   bzero (mtype, sizeof (struct type));
  567.   TYPE_MAIN_VARIANT (mtype) = mtype;
  568.   TYPE_TARGET_TYPE (mtype) = type;
  569.   TYPE_FLAGS (mtype) = TYPE_FLAG_STUB;
  570.   TYPE_CODE (mtype) = TYPE_CODE_METHOD;
  571.   TYPE_LENGTH (mtype) = 1;
  572.   return mtype;
  573. }
  574.  
  575. /* Lookup a method type returning type TYPE, belonging
  576.    to class DOMAIN, and taking a list of arguments ARGS.
  577.    If one is not found, allocate a new one.  */
  578.  
  579. struct type *
  580. lookup_method_type (type, domain, args)
  581.      struct type *type, *domain, **args;
  582. {
  583.   register struct type *mtype = TYPE_MAIN_VARIANT (type);
  584.   struct type *main_type;
  585.  
  586.   main_type = mtype;
  587.   while (mtype)
  588.     {
  589.       if (TYPE_DOMAIN_TYPE (mtype) == domain)
  590.     {
  591.       struct type **t1 = args;
  592.       struct type **t2 = TYPE_ARG_TYPES (mtype);
  593.       if (t2)
  594.         {
  595.           int i;
  596.           for (i = 0; t1[i] != 0 && t1[i]->code != TYPE_CODE_VOID; i++)
  597.         if (t1[i] != t2[i])
  598.           break;
  599.           if (t1[i] == t2[i])
  600.         return mtype;
  601.         }
  602.     }
  603.       mtype = TYPE_NEXT_VARIANT (mtype);
  604.     }
  605.  
  606.   /* This is the first time anyone wanted this member type.  */
  607.   if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
  608.     mtype  = (struct type *) xmalloc (sizeof (struct type));
  609.   else
  610.     mtype  = (struct type *) obstack_alloc (symbol_obstack,
  611.                         sizeof (struct type));
  612.  
  613.   bzero (mtype, sizeof (struct type));
  614.   if (main_type == 0)
  615.     main_type = mtype;
  616.   else
  617.     {
  618.       TYPE_NEXT_VARIANT (mtype) = TYPE_NEXT_VARIANT (main_type);
  619.       TYPE_NEXT_VARIANT (main_type) = mtype;
  620.     }
  621.   TYPE_MAIN_VARIANT (mtype) = main_type;
  622.   TYPE_TARGET_TYPE (mtype) = type;
  623.   TYPE_DOMAIN_TYPE (mtype) = domain;
  624.   TYPE_ARG_TYPES (mtype) = args;
  625.   /* New type is permanent if type pointed to is permanent.  */
  626.   if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
  627.     TYPE_FLAGS (mtype) |= TYPE_FLAG_PERM;
  628.  
  629.   /* In practice, this is never used.  */
  630.   TYPE_LENGTH (mtype) = 1;
  631.   TYPE_CODE (mtype) = TYPE_CODE_METHOD;
  632.  
  633. #if 0
  634.   /* Now splice in the new member pointer type.  */
  635.   if (main_type)
  636.     {
  637.       /* This type was not "smashed".  */
  638.       TYPE_CHAIN (mtype) = TYPE_CHAIN (main_type);
  639.       TYPE_CHAIN (main_type) = mtype;
  640.     }
  641. #endif
  642.  
  643.   return mtype;
  644. }
  645.  
  646. #if 0
  647. /* Given a type TYPE, return a type which has offset OFFSET,
  648.    via_virtual VIA_VIRTUAL, and via_public VIA_PUBLIC.
  649.    May need to construct such a type if none exists.  */
  650. struct type *
  651. lookup_basetype_type (type, offset, via_virtual, via_public)
  652.      struct type *type;
  653.      int offset;
  654.      int via_virtual, via_public;
  655. {
  656.   register struct type *btype = TYPE_MAIN_VARIANT (type);
  657.   struct type *main_type;
  658.  
  659.   if (offset != 0)
  660.     {
  661.       printf ("Internal error: type offset non-zero in lookup_basetype_type");
  662.       offset = 0;
  663.     }
  664.  
  665.   main_type = btype;
  666.   while (btype)
  667.     {
  668.       if (/* TYPE_OFFSET (btype) == offset
  669.       && */ TYPE_VIA_PUBLIC (btype) == via_public
  670.       && TYPE_VIA_VIRTUAL (btype) == via_virtual)
  671.     return btype;
  672.       btype = TYPE_NEXT_VARIANT (btype);
  673.     }
  674.  
  675.   /* This is the first time anyone wanted this member type.  */
  676.   if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
  677.     btype  = (struct type *) xmalloc (sizeof (struct type));
  678.   else
  679.     btype  = (struct type *) obstack_alloc (symbol_obstack,
  680.                         sizeof (struct type));
  681.  
  682.   if (main_type == 0)
  683.     {
  684.       main_type = btype;
  685.       bzero (btype, sizeof (struct type));
  686.       TYPE_MAIN_VARIANT (btype) = main_type;
  687.     }
  688.   else
  689.     {
  690.       bcopy (main_type, btype, sizeof (struct type));
  691.       TYPE_NEXT_VARIANT (main_type) = btype;
  692.     }
  693. /* TYPE_OFFSET (btype) = offset; */
  694.   if (via_public)
  695.     TYPE_FLAGS (btype) |= TYPE_FLAG_VIA_PUBLIC;
  696.   if (via_virtual)
  697.     TYPE_FLAGS (btype) |= TYPE_FLAG_VIA_VIRTUAL;
  698.   /* New type is permanent if type pointed to is permanent.  */
  699.   if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
  700.     TYPE_FLAGS (btype) |= TYPE_FLAG_PERM;
  701.  
  702.   /* In practice, this is never used.  */
  703.   TYPE_LENGTH (btype) = 1;
  704.   TYPE_CODE (btype) = TYPE_CODE_STRUCT;
  705.  
  706.   return btype;
  707. }
  708. #endif
  709.  
  710. /* Given a type TYPE, return a type of functions that return that type.
  711.    May need to construct such a type if this is the first use.  */
  712.  
  713. struct type *
  714. lookup_function_type (type)
  715.      struct type *type;
  716. {
  717.   register struct type *ptype = TYPE_FUNCTION_TYPE (type);
  718.   if (ptype) return ptype;
  719.  
  720.   /* This is the first time anyone wanted a function returning a TYPE.  */
  721.   if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
  722.     ptype  = (struct type *) xmalloc (sizeof (struct type));
  723.   else
  724.     ptype  = (struct type *) obstack_alloc (symbol_obstack,
  725.                         sizeof (struct type));
  726.  
  727.   bzero (ptype, sizeof (struct type));
  728.   TYPE_TARGET_TYPE (ptype) = type;
  729.   TYPE_FUNCTION_TYPE (type) = ptype;
  730.   /* New type is permanent if type returned is permanent.  */
  731.   if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
  732.     TYPE_FLAGS (ptype) |= TYPE_FLAG_PERM;
  733.   TYPE_LENGTH (ptype) = 1;
  734.   TYPE_CODE (ptype) = TYPE_CODE_FUNC;
  735.   TYPE_NFIELDS (ptype) = 0;
  736.   return ptype;
  737. }
  738.  
  739. /* Create an array type.  Elements will be of type TYPE, and there will
  740.    be NUM of them.
  741.  
  742.    Eventually this should be extended to take two more arguments which
  743.    specify the bounds of the array and the type of the index.
  744.    It should also be changed to be a "lookup" function, with the
  745.    appropriate data structures added to the type field.
  746.    Then read array type should call here.  */
  747.  
  748. struct type *
  749. create_array_type (element_type, number)
  750.      struct type *element_type;
  751.      int number;
  752. {
  753.   struct type *result_type = (struct type *)
  754.     obstack_alloc (symbol_obstack, sizeof (struct type));
  755.  
  756.   bzero (result_type, sizeof (struct type));
  757.  
  758.   TYPE_CODE (result_type) = TYPE_CODE_ARRAY;
  759.   TYPE_TARGET_TYPE (result_type) = element_type;
  760.   TYPE_LENGTH (result_type) = number * TYPE_LENGTH (element_type);
  761.   TYPE_NFIELDS (result_type) = 1;
  762.   TYPE_FIELDS (result_type) =
  763.     (struct field *) obstack_alloc (symbol_obstack, sizeof (struct field));
  764.   TYPE_FIELD_TYPE (result_type, 0) = builtin_type_int;
  765.   TYPE_VPTR_FIELDNO (result_type) = -1;
  766.  
  767.   return result_type;
  768. }
  769.  
  770.  
  771. /* Smash TYPE to be a type of members of DOMAIN with type TO_TYPE.  */
  772.  
  773. void
  774. smash_to_member_type (type, domain, to_type)
  775.      struct type *type, *domain, *to_type;
  776. {
  777.   bzero (type, sizeof (struct type));
  778.   TYPE_TARGET_TYPE (type) = to_type;
  779.   TYPE_DOMAIN_TYPE (type) = domain;
  780.  
  781.   /* In practice, this is never needed.  */
  782.   TYPE_LENGTH (type) = 1;
  783.   TYPE_CODE (type) = TYPE_CODE_MEMBER;
  784.  
  785.   TYPE_MAIN_VARIANT (type) = lookup_member_type (domain, to_type);
  786. }
  787.  
  788. /* Smash TYPE to be a type of method of DOMAIN with type TO_TYPE.  */
  789.  
  790. void
  791. smash_to_method_type (type, domain, to_type, args)
  792.      struct type *type, *domain, *to_type, **args;
  793. {
  794.   bzero (type, sizeof (struct type));
  795.   TYPE_TARGET_TYPE (type) = to_type;
  796.   TYPE_DOMAIN_TYPE (type) = domain;
  797.   TYPE_ARG_TYPES (type) = args;
  798.  
  799.   /* In practice, this is never needed.  */
  800.   TYPE_LENGTH (type) = 1;
  801.   TYPE_CODE (type) = TYPE_CODE_METHOD;
  802.  
  803.   TYPE_MAIN_VARIANT (type) = lookup_method_type (domain, to_type, args);
  804. }
  805.  
  806. /* Find which partial symtab on the partial_symtab_list contains
  807.    PC.  Return 0 if none.  */
  808.  
  809. struct partial_symtab *
  810. find_pc_psymtab (pc)
  811.      register CORE_ADDR pc;
  812. {
  813.   register struct partial_symtab *ps;
  814.  
  815.   for (ps = partial_symtab_list; ps; ps = ps->next)
  816.     if (pc >= ps->textlow && pc < ps->texthigh)
  817.       return ps;
  818.  
  819.   return 0;
  820. }
  821.  
  822. /* Find which partial symbol within a psymtab contains PC.  Return 0
  823.    if none.  Check all psymtabs if PSYMTAB is 0.  */
  824. struct partial_symbol *
  825. find_pc_psymbol (psymtab, pc)
  826.      struct partial_symtab *psymtab;
  827.      CORE_ADDR pc;
  828. {
  829.   struct partial_symbol *best, *p;
  830.   CORE_ADDR best_pc;
  831.   
  832.   if (!psymtab)
  833.     psymtab = find_pc_psymtab (pc);
  834.   if (!psymtab)
  835.     return 0;
  836.  
  837.   best_pc = psymtab->textlow - 1;
  838.  
  839.   for (p = static_psymbols.list + psymtab->statics_offset;
  840.        (p - (static_psymbols.list + psymtab->statics_offset)
  841.     < psymtab->n_static_syms);
  842.        p++)
  843.     if (SYMBOL_NAMESPACE (p) == VAR_NAMESPACE
  844.     && SYMBOL_CLASS (p) == LOC_BLOCK
  845.     && pc >= SYMBOL_VALUE_ADDRESS (p)
  846.     && SYMBOL_VALUE_ADDRESS (p) > best_pc)
  847.       {
  848.     best_pc = SYMBOL_VALUE_ADDRESS (p);
  849.     best = p;
  850.       }
  851.   if (best_pc == psymtab->textlow - 1)
  852.     return 0;
  853.   return best;
  854. }
  855.  
  856.  
  857. /* Find the definition for a specified symbol name NAME
  858.    in namespace NAMESPACE, visible from lexical block BLOCK.
  859.    Returns the struct symbol pointer, or zero if no symbol is found.
  860.    If SYMTAB is non-NULL, store the symbol table in which the
  861.    symbol was found there, or NULL if not found.
  862.    C++: if IS_A_FIELD_OF_THIS is nonzero on entry, check to see if
  863.    NAME is a field of the current implied argument `this'.  If so set
  864.    *IS_A_FIELD_OF_THIS to 1, otherwise set it to zero. 
  865.    BLOCK_FOUND is set to the block in which NAME is found (in the case of
  866.    a field of `this', value_of_this sets BLOCK_FOUND to the proper value.) */
  867.  
  868. struct symbol *
  869. lookup_symbol (name, block, namespace, is_a_field_of_this, symtab)
  870.      char *name;
  871.      register struct block *block;
  872.      enum namespace namespace;
  873.      int *is_a_field_of_this;
  874.      struct symtab **symtab;
  875. {
  876.   register struct symbol *sym;
  877.   register struct symtab *s;
  878.   register struct partial_symtab *ps;
  879.   struct blockvector *bv;
  880.  
  881.   /* Search specified block and its superiors.  */
  882.  
  883.   while (block != 0)
  884.     {
  885.       sym = lookup_block_symbol (block, name, namespace);
  886.       if (sym) 
  887.     {
  888.       block_found = block;
  889.       if (symtab != NULL)
  890.         {
  891.           /* Search the list of symtabs for one which contains the
  892.          address of the start of this block.  */
  893.           struct block *b;
  894.           for (s = symtab_list; s; s = s->next)
  895.         {
  896.           bv = BLOCKVECTOR (s);
  897.           b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
  898.           if (BLOCK_START (b) <= BLOCK_START (block)
  899.               && BLOCK_END (b) > BLOCK_START (block))
  900.             break;
  901.         }
  902.           *symtab = s;
  903.         }
  904.  
  905.       return sym;
  906.     }
  907.       block = BLOCK_SUPERBLOCK (block);
  908.     }
  909.  
  910.   /* But that doesn't do any demangling for the STATIC_BLOCK.
  911.      I'm not sure whether demangling is needed in the case of
  912.      nested function in inner blocks; if so this needs to be changed.
  913.      
  914.      Don't need to mess with the psymtabs; if we have a block,
  915.      that file is read in.  If we don't, then we deal later with
  916.      all the psymtab stuff that needs checking.  */
  917.   if (namespace == VAR_NAMESPACE && block != NULL)
  918.     {
  919.       struct block *b;
  920.       /* Find the right symtab.  */
  921.       for (s = symtab_list; s; s = s->next)
  922.     {
  923.       bv = BLOCKVECTOR (s);
  924.       b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
  925.       if (BLOCK_START (b) <= BLOCK_START (block)
  926.           && BLOCK_END (b) > BLOCK_START (block))
  927.         {
  928.           sym = lookup_demangled_block_symbol (b, name);
  929.           if (sym)
  930.         {
  931.           block_found = b;
  932.           if (symtab != NULL)
  933.             *symtab = s;
  934.           return sym;
  935.         }
  936.         }
  937.     }
  938.     }
  939.  
  940.  
  941.   /* C++: If requested to do so by the caller, 
  942.      check to see if NAME is a field of `this'. */
  943.   if (is_a_field_of_this)
  944.     {
  945.       struct value *v = value_of_this (0);
  946.       
  947.       *is_a_field_of_this = 0;
  948.       if (v && check_field (v, name))
  949.     {
  950.       *is_a_field_of_this = 1;
  951.       if (symtab != NULL)
  952.         *symtab = NULL;
  953.       return 0;
  954.     }
  955.     }
  956.  
  957.   /* Now search all global blocks.  Do the symtab's first, then
  958.      check the psymtab's */
  959.  
  960.   for (s = symtab_list; s; s = s->next)
  961.     {
  962.       bv = BLOCKVECTOR (s);
  963.       block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
  964.       sym = lookup_block_symbol (block, name, namespace);
  965.       if (sym) 
  966.     {
  967.       block_found = block;
  968.       if (symtab != NULL)
  969.         *symtab = s;
  970.       return sym;
  971.     }
  972.     }
  973.  
  974.   /* Check for the possibility of the symbol being a global function
  975.      that is stored on the misc function vector.  Eventually, all
  976.      global symbols might be resolved in this way.  */
  977.   
  978.   if (namespace == VAR_NAMESPACE)
  979.     {
  980.       int ind = lookup_misc_func (name);
  981.  
  982.       /* Look for a mangled C++ name for NAME. */
  983.       if (ind == -1)
  984.     {
  985.       int name_len = strlen (name);
  986.  
  987.       for (ind = misc_function_count; --ind >= 0; )
  988.           /* Assume orginal name is prefix of mangled name. */
  989.           if (!strncmp (misc_function_vector[ind].name, name, name_len))
  990.         {
  991.           char *demangled =
  992.               cplus_demangle(misc_function_vector[ind].name, -1);
  993.           if (demangled != NULL)
  994.             {
  995.               int cond = strcmp (demangled, name);
  996.               free (demangled);
  997.               if (!cond)
  998.               break;
  999.             }
  1000.             }
  1001.       /* Loop terminates on no match with ind == -1. */
  1002.         }
  1003.  
  1004.       if (ind != -1)
  1005.     {
  1006.       s = find_pc_symtab (misc_function_vector[ind].address);
  1007.       /* If S is zero, there are no debug symbols for this file.
  1008.          Skip this stuff and check for matching static symbols below. */
  1009.       if (s)
  1010.         {
  1011.           bv = BLOCKVECTOR (s);
  1012.           block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
  1013.           sym = lookup_block_symbol (block, misc_function_vector[ind].name,
  1014.                          namespace);
  1015.           /* sym == 0 if symbol was found in the misc_function_vector
  1016.          but not in the symtab.
  1017.          Return 0 to use the misc_function definition of "foo_".
  1018.  
  1019.          This happens for Fortran  "foo_" symbols,
  1020.          which are "foo" in the symtab.
  1021.  
  1022.          This can also happen if "asm" is used to make a
  1023.          regular symbol but not a debugging symbol, e.g.
  1024.          asm(".globl _main");
  1025.          asm("_main:");
  1026.          */
  1027.  
  1028.           if (symtab != NULL)
  1029.         *symtab = s;
  1030.           return sym;
  1031.         }
  1032.     }
  1033.     }
  1034.       
  1035.   for (ps = partial_symtab_list; ps; ps = ps->next)
  1036.     if (!ps->readin && lookup_partial_symbol (ps, name, 1, namespace))
  1037.       {
  1038.     s = PSYMTAB_TO_SYMTAB(ps);
  1039.     bv = BLOCKVECTOR (s);
  1040.     block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
  1041.     sym = lookup_block_symbol (block, name, namespace);
  1042.     if (!sym)
  1043.       fatal ("Internal: global symbol found in psymtab but not in symtab");
  1044.     if (symtab != NULL)
  1045.       *symtab = s;
  1046.     return sym;
  1047.       }
  1048.  
  1049.   /* Now search all per-file blocks.
  1050.      Not strictly correct, but more useful than an error.
  1051.      Do the symtabs first, then check the psymtabs */
  1052.  
  1053.   for (s = symtab_list; s; s = s->next)
  1054.     {
  1055.       bv = BLOCKVECTOR (s);
  1056.       block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
  1057.       sym = lookup_block_symbol (block, name, namespace);
  1058.       if (sym) 
  1059.     {
  1060.       block_found = block;
  1061.       if (symtab != NULL)
  1062.         *symtab = s;
  1063.       return sym;
  1064.     }
  1065.     }
  1066.  
  1067.   for (ps = partial_symtab_list; ps; ps = ps->next)
  1068.     if (!ps->readin && lookup_partial_symbol (ps, name, 0, namespace))
  1069.       {
  1070.     s = PSYMTAB_TO_SYMTAB(ps);
  1071.     bv = BLOCKVECTOR (s);
  1072.     block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
  1073.     sym = lookup_block_symbol (block, name, namespace);
  1074.     if (!sym)
  1075.       fatal ("Internal: static symbol found in psymtab but not in symtab");
  1076.     if (symtab != NULL)
  1077.       *symtab = s;
  1078.     return sym;
  1079.       }
  1080.  
  1081.   /* Now search all per-file blocks for static mangled symbols.
  1082.      Do the symtabs first, then check the psymtabs.  */
  1083.  
  1084.   if (namespace ==  VAR_NAMESPACE)
  1085.     {
  1086.       for (s = symtab_list; s; s = s->next)
  1087.     {
  1088.       bv = BLOCKVECTOR (s);
  1089.       block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
  1090.       sym = lookup_demangled_block_symbol (block, name);
  1091.       if (sym) 
  1092.         {
  1093.           block_found = block;
  1094.           if (symtab != NULL)
  1095.         *symtab = s;
  1096.           return sym;
  1097.         }
  1098.     }
  1099.  
  1100.       for (ps = partial_symtab_list; ps; ps = ps->next)
  1101.     if (!ps->readin && lookup_demangled_partial_symbol (ps, name))
  1102.       {
  1103.         s = PSYMTAB_TO_SYMTAB(ps);
  1104.         bv = BLOCKVECTOR (s);
  1105.         block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
  1106.         sym = lookup_demangled_block_symbol (block, name);
  1107.         if (!sym)
  1108.           fatal ("Internal: static symbol found in psymtab but not in symtab");
  1109.         if (symtab != NULL)
  1110.           *symtab = s;
  1111.         return sym;
  1112.       }
  1113.     }
  1114.  
  1115.   if (symtab != NULL)
  1116.     *symtab = NULL;
  1117.   return 0;
  1118. }
  1119.  
  1120. /* Look for a static demangled symbol in block BLOCK.  */
  1121.  
  1122. static struct symbol *
  1123. lookup_demangled_block_symbol (block, name)
  1124.      register struct block *block;
  1125.      char *name;
  1126. {
  1127.   register int bot, top, inc;
  1128.   register struct symbol *sym;
  1129.  
  1130.   bot = 0;
  1131.   top = BLOCK_NSYMS (block);
  1132.   inc = name[0];
  1133.  
  1134.   while (bot < top)
  1135.     {
  1136.       sym = BLOCK_SYM (block, bot);
  1137.       if (SYMBOL_NAME (sym)[0] == inc
  1138.       && SYMBOL_NAMESPACE (sym) == VAR_NAMESPACE)
  1139.     {
  1140.       char *demangled = cplus_demangle(SYMBOL_NAME (sym), -1);
  1141.       if (demangled != NULL)
  1142.         {
  1143.           int cond = strcmp (demangled, name);
  1144.           free (demangled);
  1145.           if (!cond)
  1146.             return sym;
  1147.         }
  1148.     }
  1149.       bot++;
  1150.     }
  1151.  
  1152.   return 0;
  1153. }
  1154.  
  1155. /* Look, in partial_symtab PST, for static mangled symbol NAME. */
  1156.  
  1157. static struct partial_symbol *
  1158. lookup_demangled_partial_symbol (pst, name)
  1159.      struct partial_symtab *pst;
  1160.      char *name;
  1161. {
  1162.   struct partial_symbol *start, *psym;
  1163.   int length = pst->n_static_syms;
  1164.   register int inc = name[0];
  1165.  
  1166.   if (!length)
  1167.     return (struct partial_symbol *) 0;
  1168.   
  1169.   start = static_psymbols.list + pst->statics_offset;
  1170.   for (psym = start; psym < start + length; psym++)
  1171.     {
  1172.       if (SYMBOL_NAME (psym)[0] == inc
  1173.       && SYMBOL_NAMESPACE (psym) == VAR_NAMESPACE)
  1174.     {
  1175.       char *demangled = cplus_demangle(SYMBOL_NAME (psym), -1);
  1176.       if (demangled != NULL)
  1177.         {
  1178.           int cond = strcmp (demangled, name);
  1179.           free (demangled);
  1180.           if (!cond)
  1181.             return psym;
  1182.         }
  1183.     }
  1184.     }
  1185.  
  1186.   return (struct partial_symbol *) 0;
  1187. }
  1188.  
  1189. /* Look, in partial_symtab PST, for symbol NAME.  Check the global
  1190.    symbols if GLOBAL, the static symbols if not */
  1191.  
  1192. static struct partial_symbol *
  1193. lookup_partial_symbol (pst, name, global, namespace)
  1194.      struct partial_symtab *pst;
  1195.      char *name;
  1196.      int global;
  1197.      enum namespace namespace;
  1198. {
  1199.   struct partial_symbol *start, *psym;
  1200.   int length = (global ? pst->n_global_syms : pst->n_static_syms);
  1201.  
  1202.   if (!length)
  1203.     return (struct partial_symbol *) 0;
  1204.   
  1205.   start = (global ?
  1206.        global_psymbols.list + pst->globals_offset :
  1207.        static_psymbols.list + pst->statics_offset  );
  1208.  
  1209.   if (global)            /* This means we can use a binary */
  1210.                 /* search.  */
  1211.     {
  1212.       struct partial_symbol *top, *bottom, *center;
  1213.  
  1214.       /* Binary search.  This search is guaranteed to end with center
  1215.          pointing at the earliest partial symbol with the correct
  1216.      name.  At that point *all* partial symbols with that name
  1217.      will be checked against the correct namespace. */
  1218.       bottom = start;
  1219.       top = start + length - 1;
  1220.       while (top > bottom)
  1221.     {
  1222.       center = bottom + (top - bottom) / 2;
  1223.  
  1224.       assert (center < top);
  1225.       
  1226.       if (strcmp (SYMBOL_NAME (center), name) >= 0)
  1227.         top = center;
  1228.       else
  1229.         bottom = center + 1;
  1230.     }
  1231.       assert (top == bottom);
  1232.       
  1233.       while (!strcmp (SYMBOL_NAME (top), name))
  1234.     {
  1235.       if (SYMBOL_NAMESPACE (top) == namespace)
  1236.         return top;
  1237.       top ++;
  1238.     }
  1239.     }
  1240.   else
  1241.     {
  1242.       /* Can't use a binary search */
  1243.       for (psym = start; psym < start + length; psym++)
  1244.     if (namespace == SYMBOL_NAMESPACE (psym)
  1245.         && !strcmp (name, SYMBOL_NAME (psym)))
  1246.       return psym;
  1247.     }
  1248.  
  1249.   return (struct partial_symbol *) 0;
  1250. }
  1251.  
  1252. /* Look for a symbol in block BLOCK.  */
  1253.  
  1254. struct symbol *
  1255. lookup_block_symbol (block, name, namespace)
  1256.      register struct block *block;
  1257.      char *name;
  1258.      enum namespace namespace;
  1259. {
  1260.   register int bot, top, inc;
  1261.   register struct symbol *sym, *parameter_sym;
  1262.  
  1263.   top = BLOCK_NSYMS (block);
  1264.   bot = 0;
  1265.  
  1266.   /* If the blocks's symbols were sorted, start with a binary search.  */
  1267.  
  1268.   if (BLOCK_SHOULD_SORT (block))
  1269.     {
  1270.       /* First, advance BOT to not far before
  1271.      the first symbol whose name is NAME.  */
  1272.  
  1273.       while (1)
  1274.     {
  1275.       inc = (top - bot + 1);
  1276.       /* No need to keep binary searching for the last few bits worth.  */
  1277.       if (inc < 4)
  1278.         break;
  1279.       inc = (inc >> 1) + bot;
  1280.       sym = BLOCK_SYM (block, inc);
  1281.       if (SYMBOL_NAME (sym)[0] < name[0])
  1282.         bot = inc;
  1283.       else if (SYMBOL_NAME (sym)[0] > name[0])
  1284.         top = inc;
  1285.       else if (strcmp (SYMBOL_NAME (sym), name) < 0)
  1286.         bot = inc;
  1287.       else
  1288.         top = inc;
  1289.     }
  1290.  
  1291.       /* Now scan forward until we run out of symbols,
  1292.      find one whose name is greater than NAME,
  1293.      or find one we want.
  1294.      If there is more than one symbol with the right name and namespace,
  1295.      we return the first one.  dbxread.c is careful to make sure
  1296.      that if one is a register then it comes first.  */
  1297.  
  1298.       top = BLOCK_NSYMS (block);
  1299.       while (bot < top)
  1300.     {
  1301.       sym = BLOCK_SYM (block, bot);
  1302.       inc = SYMBOL_NAME (sym)[0] - name[0];
  1303.       if (inc == 0)
  1304.         inc = strcmp (SYMBOL_NAME (sym), name);
  1305.       if (inc == 0 && SYMBOL_NAMESPACE (sym) == namespace)
  1306.         return sym;
  1307.       if (inc > 0)
  1308.         return 0;
  1309.       bot++;
  1310.     }
  1311.       return 0;
  1312.     }
  1313.  
  1314.   /* Here if block isn't sorted.
  1315.      This loop is equivalent to the loop above,
  1316.      but hacked greatly for speed.
  1317.  
  1318.      Note that parameter symbols do not always show up last in the
  1319.      list; this loop makes sure to take anything else other than
  1320.      parameter symbols first; it only uses parameter symbols as a
  1321.      last resort.  Note that this only takes up extra computation
  1322.      time on a match.  */
  1323.  
  1324.   parameter_sym = (struct symbol *) 0;
  1325.   top = BLOCK_NSYMS (block);
  1326.   inc = name[0];
  1327.   while (bot < top)
  1328.     {
  1329.       sym = BLOCK_SYM (block, bot);
  1330.       if (SYMBOL_NAME (sym)[0] == inc
  1331.       && !strcmp (SYMBOL_NAME (sym), name)
  1332.       && SYMBOL_NAMESPACE (sym) == namespace)
  1333.     {
  1334.       if (SYMBOL_CLASS (sym) == LOC_ARG
  1335.           || SYMBOL_CLASS (sym) == LOC_LOCAL_ARG
  1336.           || SYMBOL_CLASS (sym) == LOC_REF_ARG
  1337.           || SYMBOL_CLASS (sym) == LOC_REGPARM)
  1338.         parameter_sym = sym;
  1339.       else
  1340.         return sym;
  1341.     }
  1342.       bot++;
  1343.     }
  1344.   return parameter_sym;        /* Will be 0 if not found. */
  1345. }
  1346.  
  1347. /* Return the symbol for the function which contains a specified
  1348.    lexical block, described by a struct block BL.  */
  1349.  
  1350. struct symbol *
  1351. block_function (bl)
  1352.      struct block *bl;
  1353. {
  1354.   while (BLOCK_FUNCTION (bl) == 0 && BLOCK_SUPERBLOCK (bl) != 0)
  1355.     bl = BLOCK_SUPERBLOCK (bl);
  1356.  
  1357.   return BLOCK_FUNCTION (bl);
  1358. }
  1359.  
  1360. /* Subroutine of find_pc_line */
  1361.  
  1362. struct symtab *
  1363. find_pc_symtab (pc)
  1364.      register CORE_ADDR pc;
  1365. {
  1366.   register struct block *b;
  1367.   struct blockvector *bv;
  1368.   register struct symtab *s;
  1369.   register struct partial_symtab *ps;
  1370.  
  1371.   /* Search all symtabs for one whose file contains our pc */
  1372.  
  1373.   for (s = symtab_list; s; s = s->next)
  1374.     {
  1375.       bv = BLOCKVECTOR (s);
  1376.       b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
  1377.       if (BLOCK_START (b) <= pc
  1378.       && BLOCK_END (b) > pc)
  1379.     break;
  1380.     }
  1381.  
  1382.   if (!s)
  1383.     {
  1384.       ps = find_pc_psymtab (pc);
  1385.       if (ps && ps->readin)
  1386.     printf_filtered (
  1387.       "(Internal error: pc in read in psymtab, but not in symtab.)\n");
  1388.  
  1389.       if (ps)
  1390.     s = PSYMTAB_TO_SYMTAB (ps);
  1391.     }
  1392.  
  1393.   return s;
  1394. }
  1395.  
  1396. /* Find the source file and line number for a given PC value.
  1397.    Return a structure containing a symtab pointer, a line number,
  1398.    and a pc range for the entire source line.
  1399.    The value's .pc field is NOT the specified pc.
  1400.    NOTCURRENT nonzero means, if specified pc is on a line boundary,
  1401.    use the line that ends there.  Otherwise, in that case, the line
  1402.    that begins there is used.  */
  1403.  
  1404. struct symtab_and_line
  1405. find_pc_line (pc, notcurrent)
  1406.      CORE_ADDR pc;
  1407.      int notcurrent;
  1408. {
  1409.   struct symtab *s;
  1410.   register struct linetable *l;
  1411.   register int len;
  1412.   register int i;
  1413.   register struct linetable_entry *item;
  1414.   struct symtab_and_line val;
  1415.   struct blockvector *bv;
  1416.  
  1417.   /* Info on best line seen so far, and where it starts, and its file.  */
  1418.  
  1419.   int best_line = 0;
  1420.   CORE_ADDR best_pc = 0;
  1421.   CORE_ADDR best_end = 0;
  1422.   struct symtab *best_symtab = 0;
  1423.  
  1424.   /* Store here the first line number
  1425.      of a file which contains the line at the smallest pc after PC.
  1426.      If we don't find a line whose range contains PC,
  1427.      we will use a line one less than this,
  1428.      with a range from the start of that file to the first line's pc.  */
  1429.   int alt_line = 0;
  1430.   CORE_ADDR alt_pc = 0;
  1431.   struct symtab *alt_symtab = 0;
  1432.  
  1433.   /* Info on best line seen in this file.  */
  1434.  
  1435.   int prev_line;
  1436.   CORE_ADDR prev_pc;
  1437.  
  1438.   /* Info on first line of this file.  */
  1439.  
  1440.   int first_line;
  1441.   CORE_ADDR first_pc;
  1442.  
  1443.   /* If this pc is not from the current frame,
  1444.      it is the address of the end of a call instruction.
  1445.      Quite likely that is the start of the following statement.
  1446.      But what we want is the statement containing the instruction.
  1447.      Fudge the pc to make sure we get that.  */
  1448.  
  1449.   if (notcurrent) pc -= 1;
  1450.  
  1451.   s = find_pc_symtab (pc);
  1452.   if (s == 0)
  1453.     {
  1454.       val.symtab = 0;
  1455.       val.line = 0;
  1456.       val.pc = pc;
  1457.       val.end = 0;
  1458.       return val;
  1459.     }
  1460.  
  1461.   bv = BLOCKVECTOR (s);
  1462.  
  1463.   /* Look at all the symtabs that share this blockvector.
  1464.      They all have the same apriori range, that we found was right;
  1465.      but they have different line tables.  */
  1466.  
  1467.   for (; s && BLOCKVECTOR (s) == bv; s = s->next)
  1468.     {
  1469.       /* Find the best line in this symtab.  */
  1470.       l = LINETABLE (s);
  1471.       len = l->nitems;
  1472.       prev_line = -1;
  1473.       first_line = -1;
  1474.       for (i = 0; i < len; i++)
  1475.     {
  1476.       item = &(l->item[i]);
  1477.       
  1478.       if (first_line < 0)
  1479.         {
  1480.           first_line = item->line;
  1481.           first_pc = item->pc;
  1482.         }
  1483.       /* Return the last line that did not start after PC.  */
  1484.       if (pc >= item->pc)
  1485.         {
  1486.           prev_line = item->line;
  1487.           prev_pc = item->pc;
  1488.         }
  1489.       else
  1490.         break;
  1491.     }
  1492.  
  1493.       /* Is this file's best line closer than the best in the other files?
  1494.      If so, record this file, and its best line, as best so far.  */
  1495.       if (prev_line >= 0 && prev_pc > best_pc)
  1496.     {
  1497.       best_pc = prev_pc;
  1498.       best_line = prev_line;
  1499.       best_symtab = s;
  1500.       if (i < len)
  1501.         best_end = item->pc;
  1502.       else
  1503.         best_end = 0;
  1504.     }
  1505.       /* Is this file's first line closer than the first lines of other files?
  1506.      If so, record this file, and its first line, as best alternate.  */
  1507.       if (first_line >= 0 && first_pc > pc
  1508.       && (alt_pc == 0 || first_pc < alt_pc))
  1509.     {
  1510.       alt_pc = first_pc;
  1511.       alt_line = first_line;
  1512.       alt_symtab = s;
  1513.     }
  1514.     }
  1515.   if (best_symtab == 0)
  1516.     {
  1517.       val.symtab = alt_symtab;
  1518.       val.line = alt_line - 1;
  1519.       val.pc = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK));
  1520.       val.end = alt_pc;
  1521.     }
  1522.   else
  1523.     {
  1524.       val.symtab = best_symtab;
  1525.       val.line = best_line;
  1526.       val.pc = best_pc;
  1527.       val.end = (best_end ? best_end
  1528.            : (alt_pc ? alt_pc
  1529.               : BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK))));
  1530.     }
  1531.   return val;
  1532. }
  1533.  
  1534. /* Find the PC value for a given source file and line number.
  1535.    Returns zero for invalid line number.
  1536.    The source file is specified with a struct symtab.  */
  1537.  
  1538. CORE_ADDR
  1539. find_line_pc (symtab, line)
  1540.      struct symtab *symtab;
  1541.      int line;
  1542. {
  1543.   register struct linetable *l;
  1544.   register int ind;
  1545.   int dummy;
  1546.  
  1547.   if (symtab == 0)
  1548.     return 0;
  1549.   l = LINETABLE (symtab);
  1550.   ind = find_line_common(l, line, &dummy);
  1551.   return (ind >= 0) ? l->item[ind].pc : 0;
  1552. }
  1553.  
  1554. /* Find the range of pc values in a line.
  1555.    Store the starting pc of the line into *STARTPTR
  1556.    and the ending pc (start of next line) into *ENDPTR.
  1557.    Returns 1 to indicate success.
  1558.    Returns 0 if could not find the specified line.  */
  1559.  
  1560. int
  1561. find_line_pc_range (symtab, thisline, startptr, endptr)
  1562.      struct symtab *symtab;
  1563.      int thisline;
  1564.      CORE_ADDR *startptr, *endptr;
  1565. {
  1566.   register struct linetable *l;
  1567.   register int ind;
  1568.   int exact_match;        /* did we get an exact linenumber match */
  1569.  
  1570.   if (symtab == 0)
  1571.     return 0;
  1572.  
  1573.   l = LINETABLE (symtab);
  1574.   ind = find_line_common (l, thisline, &exact_match);
  1575.   if (ind >= 0)
  1576.     {
  1577.       *startptr = l->item[ind].pc;
  1578.       /* If we have not seen an entry for the specified line,
  1579.      assume that means the specified line has zero bytes.  */
  1580.       if (!exact_match || ind == l->nitems-1)
  1581.     *endptr = *startptr;
  1582.       else
  1583.     /* Perhaps the following entry is for the following line.
  1584.        It's worth a try.  */
  1585.     if (ind+1 < l->nitems
  1586.      && l->item[ind+1].line == thisline + 1)
  1587.       *endptr = l->item[ind+1].pc;
  1588.     else
  1589.       *endptr = find_line_pc (symtab, thisline+1);
  1590.       return 1;
  1591.     }
  1592.  
  1593.   return 0;
  1594. }
  1595.  
  1596. /* Given a line table and a line number, return the index into the line
  1597.    table for the pc of the nearest line whose number is >= the specified one.
  1598.    Return -1 if none is found.  The value is >= 0 if it is an index.
  1599.  
  1600.    Set *EXACT_MATCH nonzero if the value returned is an exact match.  */
  1601.  
  1602. static int
  1603. find_line_common (l, lineno, exact_match)
  1604.      register struct linetable *l;
  1605.      register int lineno;
  1606.      int *exact_match;
  1607. {
  1608.   register int i;
  1609.   register int len;
  1610.  
  1611.   /* BEST is the smallest linenumber > LINENO so far seen,
  1612.      or 0 if none has been seen so far.
  1613.      BEST_INDEX identifies the item for it.  */
  1614.  
  1615.   int best_index = -1;
  1616.   int best = 0;
  1617.  
  1618.   if (lineno <= 0)
  1619.     return -1;
  1620.  
  1621.   len = l->nitems;
  1622.   for (i = 0; i < len; i++)
  1623.     {
  1624.       register struct linetable_entry *item = &(l->item[i]);
  1625.  
  1626.       if (item->line == lineno)
  1627.     {
  1628.       *exact_match = 1;
  1629.       return i;
  1630.     }
  1631.  
  1632.       if (item->line > lineno && (best == 0 || item->line < best))
  1633.     {
  1634.       best = item->line;
  1635.       best_index = i;
  1636.     }
  1637.     }
  1638.  
  1639.   /* If we got here, we didn't get an exact match.  */
  1640.  
  1641.   *exact_match = 0;
  1642.   return best_index;
  1643. }
  1644.  
  1645. int
  1646. find_pc_line_pc_range (pc, startptr, endptr)
  1647.      CORE_ADDR pc;
  1648.      CORE_ADDR *startptr, *endptr;
  1649. {
  1650.   struct symtab_and_line sal;
  1651.   sal = find_pc_line (pc, 0);
  1652.   *startptr = sal.pc;
  1653.   *endptr = sal.end;
  1654.   return sal.symtab != 0;
  1655. }
  1656.  
  1657. /* If P is of the form "operator[ \t]+..." where `...' is
  1658.    some legitimate operator text, return a pointer to the
  1659.    beginning of the substring of the operator text.
  1660.    Otherwise, return "".  */
  1661. static char *
  1662. operator_chars (p, end)
  1663.      char *p;
  1664.      char **end;
  1665. {
  1666.   *end = "";
  1667.   if (strncmp (p, "operator", 8))
  1668.     return *end;
  1669.   p += 8;
  1670.  
  1671.   /* Don't get faked out by `operator' being part of a longer
  1672.      identifier.  */
  1673.   if ((*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p <= 'z')
  1674.       || *p == '_' || *p == '$' || *p == '\0')
  1675.     return *end;
  1676.  
  1677.   /* Allow some whitespace between `operator' and the operator symbol.  */
  1678.   while (*p == ' ' || *p == '\t')
  1679.     p++;
  1680.  
  1681.   switch (*p)
  1682.     {
  1683.     case '!':
  1684.     case '=':
  1685.     case '*':
  1686.     case '/':
  1687.     case '%':
  1688.     case '^':
  1689.       if (p[1] == '=')
  1690.     *end = p+2;
  1691.       else
  1692.     *end = p+1;
  1693.       return p;
  1694.     case '<':
  1695.     case '>':
  1696.     case '+':
  1697.     case '-':
  1698.     case '&':
  1699.     case '|':
  1700.       if (p[1] == '=' || p[1] == p[0])
  1701.     *end = p+2;
  1702.       else
  1703.     *end = p+1;
  1704.       return p;
  1705.     case '~':
  1706.     case ',':
  1707.       *end = p+1;
  1708.       return p;
  1709.     case '(':
  1710.       if (p[1] != ')')
  1711.     error ("`operator ()' must be specified without whitespace in `()'");
  1712.       *end = p+2;
  1713.       return p;
  1714.     case '?':
  1715.       if (p[1] != ':')
  1716.     error ("`operator ?:' must be specified without whitespace in `?:'");
  1717.       *end = p+2;
  1718.       return p;
  1719.     case '[':
  1720.       if (p[1] != ']')
  1721.     error ("`operator []' must be specified without whitespace in `[]'");
  1722.       *end = p+2;
  1723.       return p;
  1724.     default:
  1725.       error ("`operator %s' not supported", p);
  1726.       break;
  1727.     }
  1728.   *end = "";
  1729.   return *end;
  1730. }
  1731.  
  1732. /* Recursive helper function for decode_line_1.
  1733.  * Look for methods named NAME in type T.
  1734.  * Return number of matches.
  1735.  * Put matches in PHYSNAMES and SYM_ARR (which better be big enough!).
  1736.  * These allocations seem to define "big enough":
  1737.  * sym_arr = (struct symbol **) alloca(TYPE_NFN_FIELDS_TOTAL (t) * sizeof(struct symbol*));
  1738.  * physnames = (char **) alloca (TYPE_NFN_FIELDS_TOTAL (t) * sizeof(char*));
  1739.  */
  1740.  
  1741. int
  1742. find_methods(t, name, physnames, sym_arr)
  1743.      struct type *t;
  1744.      char *name;
  1745.      char **physnames;
  1746.      struct symbol **sym_arr;
  1747. {
  1748.   int i1 = 0;
  1749.   int ibase;
  1750.   struct symbol *sym_class;
  1751.   char *class_name = type_name_no_tag (t);
  1752.   /* Ignore this class if it doesn't have a name.
  1753.      This prevents core dumps, but is just a workaround
  1754.      because we might not find the function in
  1755.      certain cases, such as
  1756.      struct D {virtual int f();}
  1757.      struct C : D {virtual int g();}
  1758.      (in this case g++ 1.35.1- does not put out a name
  1759.      for D as such, it defines type 19 (for example) in
  1760.      the same stab as C, and then does a
  1761.      .stabs "D:T19" and a .stabs "D:t19".
  1762.      Thus
  1763.      "break C::f" should not be looking for field f in
  1764.      the class named D, 
  1765.      but just for the field f in the baseclasses of C
  1766.      (no matter what their names).
  1767.      
  1768.      However, I don't know how to replace the code below
  1769.      that depends on knowing the name of D.  */
  1770.   if (class_name
  1771.       && (sym_class = lookup_symbol (class_name,
  1772.                      (struct block *)NULL,
  1773.                      STRUCT_NAMESPACE,
  1774.                      (int *)NULL,
  1775.                      (struct symtab **)NULL)))
  1776.     {
  1777.       int method_counter;
  1778.       t = SYMBOL_TYPE (sym_class);
  1779.       for (method_counter = TYPE_NFN_FIELDS (t) - 1;
  1780.        method_counter >= 0;
  1781.        --method_counter)
  1782.     {
  1783.       int field_counter;
  1784.       struct fn_field *f = TYPE_FN_FIELDLIST1 (t, method_counter);
  1785.  
  1786.       char *method_name = TYPE_FN_FIELDLIST_NAME (t, method_counter);
  1787.       if (!strcmp (name, method_name))
  1788.         /* Find all the fields with that name.  */
  1789.         for (field_counter = TYPE_FN_FIELDLIST_LENGTH (t, method_counter) - 1;
  1790.          field_counter >= 0;
  1791.          --field_counter)
  1792.           {
  1793.         char *phys_name;
  1794.         if (TYPE_FLAGS (TYPE_FN_FIELD_TYPE (f, field_counter)) & TYPE_FLAG_STUB)
  1795.           check_stub_method (t, method_counter, field_counter);
  1796.         phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
  1797.         physnames[i1] = (char*) alloca (strlen (phys_name) + 1);
  1798.         strcpy (physnames[i1], phys_name);
  1799.         sym_arr[i1] = lookup_symbol (phys_name,
  1800.                          SYMBOL_BLOCK_VALUE (sym_class),
  1801.                          VAR_NAMESPACE,
  1802.                          (int *) NULL,
  1803.                          (struct symtab **) NULL);
  1804.         if (sym_arr[i1]) i1++;
  1805.           }
  1806.     }
  1807.     }
  1808.   /* Only search baseclasses if there is no match yet,
  1809.    * since names in derived classes override those in baseclasses.
  1810.    */
  1811.   if (i1)
  1812.     return i1;
  1813.   for (ibase = 0; ibase < TYPE_N_BASECLASSES (t); ibase++)
  1814.     i1 += find_methods(TYPE_BASECLASS(t, ibase), name,
  1815.                physnames + i1, sym_arr + i1);
  1816.   return i1;
  1817. }
  1818.  
  1819. /* Parse a string that specifies a line number.
  1820.    Pass the address of a char * variable; that variable will be
  1821.    advanced over the characters actually parsed.
  1822.  
  1823.    The string can be:
  1824.  
  1825.    LINENUM -- that line number in current file.  PC returned is 0.
  1826.    FILE:LINENUM -- that line in that file.  PC returned is 0.
  1827.    FUNCTION -- line number of openbrace of that function.
  1828.       PC returned is the start of the function.
  1829.    VARIABLE -- line number of definition of that variable.
  1830.       PC returned is 0.
  1831.    FILE:FUNCTION -- likewise, but prefer functions in that file.
  1832.    *EXPR -- line in which address EXPR appears.
  1833.  
  1834.    FUNCTION may be an undebuggable function found in misc_function_vector.
  1835.  
  1836.    If the argument FUNFIRSTLINE is nonzero, we want the first line
  1837.    of real code inside a function when a function is specified.
  1838.  
  1839.    DEFAULT_SYMTAB specifies the file to use if none is specified.
  1840.    It defaults to current_source_symtab.
  1841.    DEFAULT_LINE specifies the line number to use for relative
  1842.    line numbers (that start with signs).  Defaults to current_source_line.
  1843.  
  1844.    Note that it is possible to return zero for the symtab
  1845.    if no file is validly specified.  Callers must check that.
  1846.    Also, the line number returned may be invalid.  */
  1847.  
  1848. struct symtabs_and_lines
  1849. decode_line_1 (argptr, funfirstline, default_symtab, default_line)
  1850.      char **argptr;
  1851.      int funfirstline;
  1852.      struct symtab *default_symtab;
  1853.      int default_line;
  1854. {
  1855.   struct symtabs_and_lines decode_line_2 ();
  1856.   struct symtabs_and_lines values;
  1857.   struct symtab_and_line val;
  1858.   register char *p, *p1;
  1859.   char *q, *q1;
  1860.   register struct symtab *s;
  1861.  
  1862.   register struct symbol *sym;
  1863.   /* The symtab that SYM was found in.  */
  1864.   struct symtab *sym_symtab;
  1865.  
  1866.   register CORE_ADDR pc;
  1867.   register int i;
  1868.   char *copy;
  1869.   struct symbol *sym_class;
  1870.   int i1;
  1871.   struct symbol **sym_arr;
  1872.   struct type *t;
  1873.   char **physnames;
  1874.   
  1875.   /* Defaults have defaults.  */
  1876.  
  1877.   if (default_symtab == 0)
  1878.     {
  1879.       default_symtab = current_source_symtab;
  1880.       default_line = current_source_line;
  1881.     }
  1882.  
  1883.   /* See if arg is *PC */
  1884.  
  1885.   if (**argptr == '*')
  1886.     {
  1887.       (*argptr)++;
  1888.       pc = parse_and_eval_address_1 (argptr);
  1889.       values.sals = (struct symtab_and_line *)
  1890.     xmalloc (sizeof (struct symtab_and_line));
  1891.       values.nelts = 1;
  1892.       values.sals[0] = find_pc_line (pc, 0);
  1893.       values.sals[0].pc = pc;
  1894.       return values;
  1895.     }
  1896.  
  1897.   /* Maybe arg is FILE : LINENUM or FILE : FUNCTION */
  1898.  
  1899.   s = 0;
  1900.  
  1901.   for (p = *argptr; *p; p++)
  1902.     {
  1903.       if (p[0] == ':' || p[0] == ' ' || p[0] == '\t')
  1904.     break;
  1905.     }
  1906.   while (p[0] == ' ' || p[0] == '\t') p++;
  1907.  
  1908.   q = operator_chars (*argptr, &q1);
  1909.   if (p[0] == ':')
  1910.     {
  1911.  
  1912.       /*  C++  */
  1913.       if (p[1] ==':')
  1914.     {
  1915.       /* Extract the class name.  */
  1916.       p1 = p;
  1917.       while (p != *argptr && p[-1] == ' ') --p;
  1918.       copy = (char *) alloca (p - *argptr + 1);
  1919.       bcopy (*argptr, copy, p - *argptr);
  1920.       copy[p - *argptr] = 0;
  1921.  
  1922.       /* Discard the class name from the arg.  */
  1923.       p = p1 + 2;
  1924.       while (*p == ' ' || *p == '\t') p++;
  1925.       *argptr = p;
  1926.  
  1927.       sym_class = lookup_symbol (copy, 0, STRUCT_NAMESPACE, 0, 
  1928.                      (struct symtab **)NULL);
  1929.        
  1930.       if (sym_class &&
  1931.           (TYPE_CODE (SYMBOL_TYPE (sym_class)) == TYPE_CODE_STRUCT
  1932.            || TYPE_CODE (SYMBOL_TYPE (sym_class)) == TYPE_CODE_UNION))
  1933.         {
  1934.           /* Arg token is not digits => try it as a function name
  1935.          Find the next token (everything up to end or next whitespace). */
  1936.           p = *argptr;
  1937.           while (*p && *p != ' ' && *p != '\t' && *p != ',' && *p !=':') p++;
  1938.           q = operator_chars (*argptr, &q1);
  1939.  
  1940.           copy = (char *) alloca (p - *argptr + 1 + (q1 - q));
  1941.           if (q1 - q)
  1942.         {
  1943.           copy[0] = 'o';
  1944.           copy[1] = 'p';
  1945.           copy[2] = CPLUS_MARKER;
  1946.           bcopy (q, copy + 3, q1 - q);
  1947.           copy[3 + (q1 - q)] = '\0';
  1948.           p = q1;
  1949.         }
  1950.           else
  1951.         {
  1952.           bcopy (*argptr, copy, p - *argptr);
  1953.           copy[p - *argptr] = '\0';
  1954.         }
  1955.  
  1956.           /* no line number may be specified */
  1957.           while (*p == ' ' || *p == '\t') p++;
  1958.           *argptr = p;
  1959.  
  1960.           sym = 0;
  1961.           i1 = 0;        /*  counter for the symbol array */
  1962.           t = SYMBOL_TYPE (sym_class);
  1963.           sym_arr = (struct symbol **) alloca(TYPE_NFN_FIELDS_TOTAL (t) * sizeof(struct symbol*));
  1964.           physnames = (char **) alloca (TYPE_NFN_FIELDS_TOTAL (t) * sizeof(char*));
  1965.  
  1966.           if (destructor_name_p (copy, t))
  1967.         {
  1968.           /* destructors are a special case.  */
  1969.           struct fn_field *f = TYPE_FN_FIELDLIST1 (t, 0);
  1970.           int len = TYPE_FN_FIELDLIST_LENGTH (t, 0) - 1;
  1971.           char *phys_name = TYPE_FN_FIELD_PHYSNAME (f, len);
  1972.           physnames[i1] = (char *)alloca (strlen (phys_name) + 1);
  1973.           strcpy (physnames[i1], phys_name);
  1974.           sym_arr[i1] =
  1975.             lookup_symbol (phys_name, SYMBOL_BLOCK_VALUE (sym_class),
  1976.                    VAR_NAMESPACE, 0, (struct symtab **)NULL);
  1977.           if (sym_arr[i1]) i1++;
  1978.         }
  1979.           else
  1980.         i1 = find_methods (t, copy, physnames, sym_arr);
  1981.           if (i1 == 1)
  1982.         {
  1983.           /* There is exactly one field with that name.  */
  1984.           sym = sym_arr[0];
  1985.  
  1986.           if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
  1987.             {
  1988.               /* Arg is the name of a function */
  1989.               pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) + FUNCTION_START_OFFSET;
  1990.               if (funfirstline)
  1991.             SKIP_PROLOGUE (pc);
  1992.               values.sals = (struct symtab_and_line *)xmalloc (sizeof (struct symtab_and_line));
  1993.               values.nelts = 1;
  1994.               values.sals[0] = find_pc_line (pc, 0);
  1995.               values.sals[0].pc = (values.sals[0].end && values.sals[0].pc != pc) ? values.sals[0].end : pc;
  1996.             }
  1997.           else
  1998.             {
  1999.               values.nelts = 0;
  2000.             }
  2001.           return values;
  2002.         }
  2003.           if (i1 > 0)
  2004.         {
  2005.           /* There is more than one field with that name
  2006.              (overloaded).  Ask the user which one to use.  */
  2007.           return decode_line_2 (sym_arr, i1, funfirstline);
  2008.         }
  2009.           else
  2010.         {
  2011.           char *tmp;
  2012.  
  2013.           if (OPNAME_PREFIX_P (copy))
  2014.             {
  2015.               tmp = (char *)alloca (strlen (copy+3) + 9);
  2016.               strcpy (tmp, "operator ");
  2017.               strcat (tmp, copy+3);
  2018.             }
  2019.           else
  2020.             tmp = copy;
  2021.           error ("that class does not have any method named %s", tmp);
  2022.         }
  2023.         }
  2024.       else
  2025.         /* The quotes are important if copy is empty.  */
  2026.         error("No class, struct, or union named \"%s\"", copy );
  2027.     }
  2028.       /*  end of C++  */
  2029.  
  2030.  
  2031.       /* Extract the file name.  */
  2032.       p1 = p;
  2033.       while (p != *argptr && p[-1] == ' ') --p;
  2034.       copy = (char *) alloca (p - *argptr + 1 + (q1 - q));
  2035.       if (q1 - q)
  2036.     {
  2037.       copy[0] = 'o';
  2038.       copy[1] = 'p';
  2039.       copy[2] = CPLUS_MARKER;
  2040.       bcopy (q, copy + 3, q1-q);
  2041.       copy[3 + (q1-q)] = 0;
  2042.       p = q1;
  2043.     }
  2044.       else
  2045.     {
  2046.       bcopy (*argptr, copy, p - *argptr);
  2047.       copy[p - *argptr] = 0;
  2048.     }
  2049.  
  2050.       /* Find that file's data.  */
  2051.       s = lookup_symtab (copy);
  2052.       if (s == 0)
  2053.     {
  2054.       if (symtab_list == 0 && partial_symtab_list == 0)
  2055.         error (no_symtab_msg);
  2056.       error ("No source file named %s.", copy);
  2057.     }
  2058.  
  2059.       /* Discard the file name from the arg.  */
  2060.       p = p1 + 1;
  2061.       while (*p == ' ' || *p == '\t') p++;
  2062.       *argptr = p;
  2063.     }
  2064.  
  2065.   /* S is specified file's symtab, or 0 if no file specified.
  2066.      arg no longer contains the file name.  */
  2067.  
  2068.   /* Check whether arg is all digits (and sign) */
  2069.  
  2070.   p = *argptr;
  2071.   if (*p == '-' || *p == '+') p++;
  2072.   while (*p >= '0' && *p <= '9')
  2073.     p++;
  2074.  
  2075.   if (p != *argptr && (*p == 0 || *p == ' ' || *p == '\t' || *p == ','))
  2076.     {
  2077.       /* We found a token consisting of all digits -- at least one digit.  */
  2078.       enum sign {none, plus, minus} sign = none;
  2079.  
  2080.       /* This is where we need to make sure that we have good defaults.
  2081.      We must guarantee that this section of code is never executed
  2082.      when we are called with just a function name, since
  2083.      select_source_symtab calls us with such an argument  */
  2084.  
  2085.       if (s == 0 && default_symtab == 0)
  2086.     {
  2087.       select_source_symtab (0);
  2088.       default_symtab = current_source_symtab;
  2089.       default_line = current_source_line;
  2090.     }
  2091.  
  2092.       if (**argptr == '+')
  2093.     sign = plus, (*argptr)++;
  2094.       else if (**argptr == '-')
  2095.     sign = minus, (*argptr)++;
  2096.       val.line = atoi (*argptr);
  2097.       switch (sign)
  2098.     {
  2099.     case plus:
  2100.       if (p == *argptr)
  2101.         val.line = 5;
  2102.       if (s == 0)
  2103.         val.line = default_line + val.line;
  2104.       break;
  2105.     case minus:
  2106.       if (p == *argptr)
  2107.         val.line = 15;
  2108.       if (s == 0)
  2109.         val.line = default_line - val.line;
  2110.       else
  2111.         val.line = 1;
  2112.       break;
  2113.     case none:
  2114.       break;    /* No need to adjust val.line.  */
  2115.     }
  2116.  
  2117.       while (*p == ' ' || *p == '\t') p++;
  2118.       *argptr = p;
  2119.       if (s == 0)
  2120.     s = default_symtab;
  2121.       val.symtab = s;
  2122.       val.pc = 0;
  2123.       values.sals = (struct symtab_and_line *)xmalloc (sizeof (struct symtab_and_line));
  2124.       values.sals[0] = val;
  2125.       values.nelts = 1;
  2126.       return values;
  2127.     }
  2128.  
  2129.   /* Arg token is not digits => try it as a variable name
  2130.      Find the next token (everything up to end or next whitespace).  */
  2131.   p = *argptr;
  2132.   while (*p && *p != ' ' && *p != '\t' && *p != ',') p++;
  2133.   copy = (char *) alloca (p - *argptr + 1);
  2134.   bcopy (*argptr, copy, p - *argptr);
  2135.   copy[p - *argptr] = 0;
  2136.   while (*p == ' ' || *p == '\t') p++;
  2137.   *argptr = p;
  2138.  
  2139.   /* Look up that token as a variable.
  2140.      If file specified, use that file's per-file block to start with.  */
  2141.  
  2142.   sym = lookup_symbol (copy,
  2143.                (s ? BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK)
  2144.             : get_selected_block ()),
  2145.                VAR_NAMESPACE, 0, &sym_symtab);
  2146.  
  2147.   if (sym != NULL)
  2148.     {
  2149.       if (SYMBOL_CLASS (sym) == LOC_BLOCK)
  2150.     {
  2151.       /* Arg is the name of a function */
  2152.       pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) + FUNCTION_START_OFFSET;
  2153.       if (funfirstline)
  2154.         SKIP_PROLOGUE (pc);
  2155.       val = find_pc_line (pc, 0);
  2156. #ifdef PROLOGUE_FIRSTLINE_OVERLAP
  2157.       /* Convex: no need to suppress code on first line, if any */
  2158.       val.pc = pc;
  2159. #else
  2160.       val.pc = (val.end && val.pc != pc) ? val.end : pc;
  2161. #endif
  2162.       values.sals = (struct symtab_and_line *)xmalloc (sizeof (struct symtab_and_line));
  2163.       values.sals[0] = val;
  2164.       values.nelts = 1;
  2165.       
  2166.       /* I think this is always the same as the line that
  2167.          we calculate above, but the general principle is
  2168.          "trust the symbols more than stuff like
  2169.          SKIP_PROLOGUE".  */
  2170.       if (SYMBOL_LINE (sym) != 0)
  2171.         values.sals[0].line = SYMBOL_LINE (sym);
  2172.       
  2173.       return values;
  2174.     }
  2175.       else if (SYMBOL_LINE (sym) != 0)
  2176.     {
  2177.       /* We know its line number.  */
  2178.       values.sals = (struct symtab_and_line *)
  2179.         xmalloc (sizeof (struct symtab_and_line));
  2180.       values.nelts = 1;
  2181.       bzero (&values.sals[0], sizeof (values.sals[0]));
  2182.       values.sals[0].symtab = sym_symtab;
  2183.       values.sals[0].line = SYMBOL_LINE (sym);
  2184.       return values;
  2185.     }
  2186.       else
  2187.     /* This can happen if it is compiled with a compiler which doesn't
  2188.        put out line numbers for variables.  */
  2189.     error ("Line number not known for symbol \"%s\"", copy);
  2190.     }
  2191.  
  2192.   if ((i = lookup_misc_func (copy)) >= 0)
  2193.     {
  2194.       val.symtab = 0;
  2195.       val.line = 0;
  2196.       val.pc = misc_function_vector[i].address + FUNCTION_START_OFFSET;
  2197.       if (funfirstline)
  2198.     SKIP_PROLOGUE (val.pc);
  2199.       values.sals = (struct symtab_and_line *)xmalloc (sizeof (struct symtab_and_line));
  2200.       values.sals[0] = val;
  2201.       values.nelts = 1;
  2202.       return values;
  2203.     }
  2204.  
  2205.   if (symtab_list == 0 && partial_symtab_list == 0 && misc_function_count == 0)
  2206.     error (no_symtab_msg);
  2207.  
  2208.   error ("Function %s not defined.", copy);
  2209.   return values;    /* for lint */
  2210. }
  2211.  
  2212. struct symtabs_and_lines
  2213. decode_line_spec (string, funfirstline)
  2214.      char *string;
  2215.      int funfirstline;
  2216. {
  2217.   struct symtabs_and_lines sals;
  2218.   if (string == 0)
  2219.     error ("Empty line specification.");
  2220.   sals = decode_line_1 (&string, funfirstline,
  2221.             current_source_symtab, current_source_line);
  2222.   if (*string)
  2223.     error ("Junk at end of line specification: %s", string);
  2224.   return sals;
  2225. }
  2226.  
  2227. /* Given a list of NELTS symbols in sym_arr (with corresponding
  2228.    mangled names in physnames), return a list of lines to operate on
  2229.    (ask user if necessary).  */
  2230. struct symtabs_and_lines
  2231. decode_line_2 (sym_arr, nelts, funfirstline)
  2232.      struct symbol *sym_arr[];
  2233.      int nelts;
  2234.      int funfirstline;
  2235. {
  2236.   struct symtabs_and_lines values, return_values;
  2237.   register CORE_ADDR pc;
  2238.   char *args, *arg1, *command_line_input ();
  2239.   int i;
  2240.   char *prompt;
  2241.  
  2242.   values.sals = (struct symtab_and_line *) alloca (nelts * sizeof(struct symtab_and_line));
  2243.   return_values.sals = (struct symtab_and_line *) xmalloc (nelts * sizeof(struct symtab_and_line));
  2244.  
  2245.   i = 0;
  2246.   printf("[0] cancel\n[1] all\n");
  2247.   while (i < nelts)
  2248.     {
  2249.       if (sym_arr[i] && SYMBOL_CLASS (sym_arr[i]) == LOC_BLOCK)
  2250.     {
  2251.       /* Arg is the name of a function */
  2252.       pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym_arr[i])) 
  2253.            + FUNCTION_START_OFFSET;
  2254.       if (funfirstline)
  2255.         SKIP_PROLOGUE (pc);
  2256.       values.sals[i] = find_pc_line (pc, 0);
  2257.       values.sals[i].pc = (values.sals[i].end && values.sals[i].pc != pc) ?
  2258.                    values.sals[i].end                      :  pc;
  2259.       printf("[%d] file:%s; line number:%d\n",
  2260.          (i+2), values.sals[i].symtab->filename, values.sals[i].line);
  2261.     }
  2262.       else printf ("?HERE\n");
  2263.       i++;
  2264.     }
  2265.   
  2266.   if ((prompt = getenv ("PS2")) == NULL)
  2267.     {
  2268.       prompt = ">";
  2269.     }
  2270.   printf("%s ",prompt);
  2271.   fflush(stdout);
  2272.  
  2273.   args = command_line_input (0, 0);
  2274.   
  2275.   if (args == 0)
  2276.     error_no_arg ("one or more choice numbers");
  2277.  
  2278.   i = 0;
  2279.   while (*args)
  2280.     {
  2281.       int num;
  2282.  
  2283.       arg1 = args;
  2284.       while (*arg1 >= '0' && *arg1 <= '9') arg1++;
  2285.       if (*arg1 && *arg1 != ' ' && *arg1 != '\t')
  2286.     error ("Arguments must be choice numbers.");
  2287.  
  2288.       num = atoi (args);
  2289.  
  2290.       if (num == 0)
  2291.     error ("cancelled");
  2292.       else if (num == 1)
  2293.     {
  2294.       bcopy (values.sals, return_values.sals, (nelts * sizeof(struct symtab_and_line)));
  2295.       return_values.nelts = nelts;
  2296.       return return_values;
  2297.     }
  2298.  
  2299.       if (num > nelts + 2)
  2300.     {
  2301.       printf ("No choice number %d.\n", num);
  2302.     }
  2303.       else
  2304.     {
  2305.       num -= 2;
  2306.       if (values.sals[num].pc)
  2307.         {
  2308.           return_values.sals[i++] = values.sals[num];
  2309.           values.sals[num].pc = 0;
  2310.         }
  2311.       else
  2312.         {
  2313.           printf ("duplicate request for %d ignored.\n", num);
  2314.         }
  2315.     }
  2316.  
  2317.       args = arg1;
  2318.       while (*args == ' ' || *args == '\t') args++;
  2319.     }
  2320.   return_values.nelts = i;
  2321.   return return_values;
  2322. }
  2323.  
  2324. /* Return the index of misc function named NAME.  */
  2325.  
  2326. int
  2327. lookup_misc_func (name)
  2328.      register char *name;
  2329. {
  2330.   register int i;
  2331.  
  2332.   for (i = 0; i < misc_function_count; i++)
  2333.     if (!strcmp (misc_function_vector[i].name, name))
  2334.       return i;
  2335.   return -1;        /* not found */
  2336. }
  2337.  
  2338. /* Slave routine for sources_info.  Force line breaks at ,'s.
  2339.    NAME is the name to print and *FIRST is nonzero if this is the first
  2340.    name printed.  Set *FIRST to zero.  */
  2341. static void
  2342. output_source_filename (name, first)
  2343.      char *name;
  2344.      int *first;
  2345. {
  2346.   static int column;
  2347.   /* Table of files printed so far.  Since a single source file can
  2348.      result in several partial symbol tables, we need to avoid printing
  2349.      it more than once.  Note: if some of the psymtabs are read in and
  2350.      some are not, it gets printed both under "Source files for which
  2351.      symbols have been read" and "Source files for which symbols will
  2352.      be read in on demand".  I consider this a reasonable way to deal
  2353.      with the situation.  I'm not sure whether this can also happen for
  2354.      symtabs; it doesn't hurt to check.  */
  2355.   static char **tab = NULL;
  2356.   /* Allocated size of tab in elements.
  2357.      Start with one 256-byte block (when using GNU malloc.c).
  2358.      24 is the malloc overhead when range checking is in effect.  */
  2359.   static int tab_alloc_size = (256 - 24) / sizeof (char *);
  2360.   /* Current size of tab in elements.  */
  2361.   static int tab_cur_size;
  2362.  
  2363.   char **p;
  2364.  
  2365.   if (*first)
  2366.     {
  2367.       if (tab == NULL)
  2368.     tab = (char **) xmalloc (tab_alloc_size * sizeof (*tab));
  2369.       tab_cur_size = 0;
  2370.     }
  2371.  
  2372.   /* Is NAME in tab?  */
  2373.   for (p = tab; p < tab + tab_cur_size; p++)
  2374.     if (strcmp (*p, name) == 0)
  2375.       /* Yes; don't print it again.  */
  2376.       return;
  2377.   /* No; add it to tab.  */
  2378.   if (tab_cur_size == tab_alloc_size)
  2379.     {
  2380.       tab_alloc_size *= 2;
  2381.       tab = (char **) xrealloc (tab, tab_alloc_size * sizeof (*tab));
  2382.     }
  2383.   tab[tab_cur_size++] = name;
  2384.  
  2385.   if (*first)
  2386.     {
  2387.       column = 0;
  2388.       *first = 0;
  2389.     }
  2390.   else
  2391.     {
  2392.       printf_filtered (",");
  2393.       column++;
  2394.     }
  2395.  
  2396.   if (column != 0 && column + strlen (name) >= 70)
  2397.     {
  2398.       printf_filtered ("\n");
  2399.       column = 0;
  2400.     }
  2401.   else if (column != 0)
  2402.     {
  2403.       printf_filtered (" ");
  2404.       column++;
  2405.     }
  2406.   fputs_filtered (name, stdout);
  2407.   column += strlen (name);
  2408. }  
  2409.  
  2410. static void
  2411. sources_info ()
  2412. {
  2413.   register struct symtab *s;
  2414.   register struct partial_symtab *ps;
  2415.   int first;
  2416.   
  2417.   if (symtab_list == 0 && partial_symtab_list == 0)
  2418.     {
  2419.       printf (no_symtab_msg);
  2420.       return;
  2421.     }
  2422.   
  2423.   printf_filtered ("Source files for which symbols have been read in:\n\n");
  2424.  
  2425.   first = 1;
  2426.   for (s = symtab_list; s; s = s->next)
  2427.     output_source_filename (s->filename, &first);
  2428.   printf_filtered ("\n\n");
  2429.   
  2430.   printf_filtered ("Source files for which symbols will be read in on demand:\n\n");
  2431.  
  2432.   first = 1;
  2433.   for (ps = partial_symtab_list; ps; ps = ps->next)
  2434.     if (!ps->readin)
  2435.       output_source_filename (ps->filename, &first);
  2436.   printf_filtered ("\n");
  2437. }
  2438.  
  2439. /* List all symbols (if REGEXP is 0) or all symbols matching REGEXP.
  2440.    If CLASS is zero, list all symbols except functions and type names.
  2441.    If CLASS is 1, list only functions.
  2442.    If CLASS is 2, list only type names.
  2443.  
  2444.    BPT is non-zero if we should set a breakpoint at the functions
  2445.    we find.  */
  2446.  
  2447. static void
  2448. list_symbols (regexp, class, bpt)
  2449.      char *regexp;
  2450.      int class;
  2451.      int bpt;
  2452. {
  2453.   register struct symtab *s;
  2454.   register struct partial_symtab *ps;
  2455.   register struct blockvector *bv;
  2456.   struct blockvector *prev_bv = 0;
  2457.   register struct block *b;
  2458.   register int i, j;
  2459.   register struct symbol *sym;
  2460.   struct partial_symbol *psym;
  2461.   char *val;
  2462.   static char *classnames[]
  2463.     = {"variable", "function", "type", "method"};
  2464.   int found_in_file = 0;
  2465.   int found_misc = 0;
  2466.   static enum misc_function_type types[]
  2467.     = {mf_data, mf_text, mf_abs, mf_unknown};
  2468.   static enum misc_function_type types2[]
  2469.     = {mf_bss,  mf_text, mf_abs, mf_unknown};
  2470.   enum misc_function_type ourtype = types[class];
  2471.   enum misc_function_type ourtype2 = types2[class];
  2472.  
  2473.   if (regexp)
  2474.     if (0 != (val = re_comp (regexp)))
  2475.       error ("Invalid regexp (%s): %s", val, regexp);
  2476.  
  2477.   /* Search through the partial_symtab_list *first* for all symbols
  2478.      matching the regexp.  That way we don't have to reproduce all of
  2479.      the machinery below. */
  2480.   for (ps = partial_symtab_list; ps; ps = ps->next)
  2481.     {
  2482.       struct partial_symbol *bound, *gbound, *sbound;
  2483.       int keep_going = 1;
  2484.  
  2485.       if (ps->readin) continue;
  2486.       
  2487.       gbound = global_psymbols.list + ps->globals_offset + ps->n_global_syms;
  2488.       sbound = static_psymbols.list + ps->statics_offset + ps->n_static_syms;
  2489.       bound = gbound;
  2490.  
  2491.       /* Go through all of the symbols stored in a partial
  2492.      symtab in one loop. */
  2493.       psym = global_psymbols.list + ps->globals_offset;
  2494.       while (keep_going)
  2495.     {
  2496.       if (psym >= bound)
  2497.         {
  2498.           if (bound == gbound && ps->n_static_syms != 0)
  2499.         {
  2500.           psym = static_psymbols.list + ps->statics_offset;
  2501.           bound = sbound;
  2502.         }
  2503.           else
  2504.         keep_going = 0;
  2505.           continue;
  2506.         }
  2507.       else
  2508.         {
  2509.           QUIT;
  2510.  
  2511.           /* If it would match (logic taken from loop below)
  2512.          load the file and go on to the next one */
  2513.           if ((regexp == 0 || re_exec (SYMBOL_NAME (psym)))
  2514.           && ((class == 0 && SYMBOL_CLASS (psym) != LOC_TYPEDEF
  2515.                   && SYMBOL_CLASS (psym) != LOC_BLOCK)
  2516.               || (class == 1 && SYMBOL_CLASS (psym) == LOC_BLOCK)
  2517.               || (class == 2 && SYMBOL_CLASS (psym) == LOC_TYPEDEF)
  2518.               || (class == 3 && SYMBOL_CLASS (psym) == LOC_BLOCK)))
  2519.         {
  2520.           (void) PSYMTAB_TO_SYMTAB(ps);
  2521.           keep_going = 0;
  2522.         }
  2523.         }
  2524.       psym++;
  2525.     }
  2526.     }
  2527.  
  2528.   /* Here, we search through the misc function vector for functions that
  2529.      match, and call find_pc_symtab on them to force their symbols to
  2530.      be read.  The symbol will then be found during the scan of symtabs
  2531.      below.  If find_pc_symtab fails, set found_misc so that we will
  2532.      rescan to print any matching symbols without debug info.  */
  2533.  
  2534.   if (class == 1) {
  2535.     for (i = 0; i < misc_function_count; i++) {
  2536.       if (misc_function_vector[i].type != ourtype
  2537.        && misc_function_vector[i].type != ourtype2)
  2538.     continue;
  2539.       if (regexp == 0 || re_exec (misc_function_vector[i].name)) {
  2540.       if (0 == find_pc_symtab (misc_function_vector[i].address))
  2541.         found_misc = 1;
  2542.       }
  2543.     }
  2544.   }
  2545.  
  2546.   /* Printout here so as to get after the "Reading in symbols"
  2547.      messages which will be generated above.  */
  2548.   if (!bpt)
  2549.     printf_filtered (regexp
  2550.       ? "All %ss matching regular expression \"%s\":\n"
  2551.       : "All defined %ss:\n",
  2552.       classnames[class],
  2553.       regexp);
  2554.  
  2555.   for (s = symtab_list; s; s = s->next)
  2556.     {
  2557.       found_in_file = 0;
  2558.       bv = BLOCKVECTOR (s);
  2559.       /* Often many files share a blockvector.
  2560.      Scan each blockvector only once so that
  2561.      we don't get every symbol many times.
  2562.      It happens that the first symtab in the list
  2563.      for any given blockvector is the main file.  */
  2564.       if (bv != prev_bv)
  2565.     for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
  2566.       {
  2567.         b = BLOCKVECTOR_BLOCK (bv, i);
  2568.         /* Skip the sort if this block is always sorted.  */
  2569.         if (!BLOCK_SHOULD_SORT (b))
  2570.           sort_block_syms (b);
  2571.         for (j = 0; j < BLOCK_NSYMS (b); j++)
  2572.           {
  2573.         QUIT;
  2574.         sym = BLOCK_SYM (b, j);
  2575.         if ((regexp == 0 || re_exec (SYMBOL_NAME (sym)))
  2576.             && ((class == 0 && SYMBOL_CLASS (sym) != LOC_TYPEDEF
  2577.                     && SYMBOL_CLASS (sym) != LOC_BLOCK)
  2578.             || (class == 1 && SYMBOL_CLASS (sym) == LOC_BLOCK)
  2579.             || (class == 2 && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
  2580.             || (class == 3 && SYMBOL_CLASS (sym) == LOC_BLOCK)))
  2581.           {
  2582.             if (bpt)
  2583.               {
  2584.             /* Set a breakpoint here, if it's a function */
  2585.             if (class == 1)
  2586.               break_command (SYMBOL_NAME(sym), 0);
  2587.               }
  2588.             else if (!found_in_file)
  2589.               {
  2590.             fputs_filtered ("\nFile ", stdout);
  2591.             fputs_filtered (s->filename, stdout);
  2592.             fputs_filtered (":\n", stdout);
  2593.               }
  2594.             found_in_file = 1;
  2595.  
  2596.             if (class != 2 && i == STATIC_BLOCK)
  2597.               printf_filtered ("static ");
  2598.             if (class == 2
  2599.             && SYMBOL_NAMESPACE (sym) != STRUCT_NAMESPACE)
  2600.               printf_filtered ("typedef ");
  2601.  
  2602.             if (class < 3)
  2603.               {
  2604.             type_print (SYMBOL_TYPE (sym),
  2605.                     (SYMBOL_CLASS (sym) == LOC_TYPEDEF
  2606.                      ? "" : SYMBOL_NAME (sym)),
  2607.                     stdout, 0);
  2608.  
  2609.             if (class == 2
  2610.                 && SYMBOL_NAMESPACE (sym) != STRUCT_NAMESPACE
  2611.                 && (TYPE_NAME ((SYMBOL_TYPE (sym))) == 0
  2612.                 || 0 != strcmp (TYPE_NAME ((SYMBOL_TYPE (sym))),
  2613.                         SYMBOL_NAME (sym))))
  2614.               {
  2615.                 fputs_filtered (" ", stdout);
  2616.                 fprint_symbol (stdout, SYMBOL_NAME (sym));
  2617.               }
  2618.  
  2619.             printf_filtered (";\n");
  2620.               }
  2621.             else
  2622.               {
  2623. # if 0
  2624.             char buf[1024];
  2625.             type_print_base (TYPE_FN_FIELD_TYPE(t, i), stdout, 0, 0); 
  2626.             type_print_varspec_prefix (TYPE_FN_FIELD_TYPE(t, i), stdout, 0); 
  2627.             sprintf (buf, " %s::", type_name_no_tag (t));
  2628.             type_print_method_args (TYPE_FN_FIELD_ARGS (t, i), buf, name, stdout);
  2629. # endif
  2630.               }
  2631.           }
  2632.           }
  2633.       }
  2634.       prev_bv = bv;
  2635.     }
  2636.  
  2637.  
  2638.   /* If there are no eyes, avoid all contact.  I mean, if there are
  2639.      no debug symbols, then print directly from the misc_function_vector.  */
  2640.  
  2641.   if (found_misc || class != 1) {
  2642.     found_in_file = 0;
  2643.     for (i = 0; i < misc_function_count; i++) {
  2644.       if (misc_function_vector[i].type != ourtype
  2645.        && misc_function_vector[i].type != ourtype2)
  2646.     continue;
  2647.       if (regexp == 0 || re_exec (misc_function_vector[i].name)) {
  2648.     /* Functions:  Look up by address. */
  2649.     if (class == 1)
  2650.       if (0 != find_pc_symtab (misc_function_vector[i].address))
  2651.         continue;
  2652.     /* Variables/Absolutes:  Look up by name */
  2653.     if (0 != lookup_symbol (misc_function_vector[i].name, 
  2654.         (struct block *)0, VAR_NAMESPACE, 0, (struct symtab **)0))
  2655.       continue;
  2656.     if (!found_in_file) {
  2657.       printf_filtered ("\nNon-debugging symbols:\n");
  2658.       found_in_file = 1;
  2659.     }
  2660.     printf_filtered ("    %08x  %s\n",
  2661.           misc_function_vector[i].address,
  2662.           misc_function_vector[i].name);
  2663.       }
  2664.     }
  2665.   }
  2666. }
  2667.  
  2668. static void
  2669. variables_info (regexp)
  2670.      char *regexp;
  2671. {
  2672.   list_symbols (regexp, 0, 0);
  2673. }
  2674.  
  2675. static void
  2676. functions_info (regexp)
  2677.      char *regexp;
  2678. {
  2679.   list_symbols (regexp, 1, 0);
  2680. }
  2681.  
  2682. static void
  2683. types_info (regexp)
  2684.      char *regexp;
  2685. {
  2686.   list_symbols (regexp, 2, 0);
  2687. }
  2688.  
  2689. #if 0
  2690. /* Tiemann says: "info methods was never implemented."  */
  2691. static void
  2692. methods_info (regexp)
  2693.      char *regexp;
  2694. {
  2695.   list_symbols (regexp, 3, 0);
  2696. }
  2697. #endif /* 0 */
  2698.  
  2699. /* Breakpoint all functions matching regular expression. */
  2700. static void
  2701. rbreak_command (regexp)
  2702.      char *regexp;
  2703. {
  2704.   list_symbols (regexp, 1, 1);
  2705. }
  2706.  
  2707. /* Initialize the standard C scalar types.  */
  2708.  
  2709. static
  2710. struct type *
  2711. init_type (code, length, uns, name)
  2712.      enum type_code code;
  2713.      int length, uns;
  2714.      char *name;
  2715. {
  2716.   register struct type *type;
  2717.  
  2718.   type = (struct type *) xmalloc (sizeof (struct type));
  2719.   bzero (type, sizeof *type);
  2720.   TYPE_MAIN_VARIANT (type) = type;
  2721.   TYPE_CODE (type) = code;
  2722.   TYPE_LENGTH (type) = length;
  2723.   TYPE_FLAGS (type) = uns ? TYPE_FLAG_UNSIGNED : 0;
  2724.   TYPE_FLAGS (type) |= TYPE_FLAG_PERM;
  2725.   TYPE_NFIELDS (type) = 0;
  2726.   TYPE_NAME (type) = name;
  2727.  
  2728.   /* C++ fancies.  */
  2729.   TYPE_NFN_FIELDS (type) = 0;
  2730.   TYPE_N_BASECLASSES (type) = 0;
  2731.   return type;
  2732. }
  2733.  
  2734. /* Return Nonzero if block a is lexically nested within block b,
  2735.    or if a and b have the same pc range.
  2736.    Return zero otherwise. */
  2737. int
  2738. contained_in (a, b)
  2739.      struct block *a, *b;
  2740. {
  2741.   if (!a || !b)
  2742.     return 0;
  2743.   return BLOCK_START (a) >= BLOCK_START (b)
  2744.       && BLOCK_END (a)   <= BLOCK_END (b);
  2745. }
  2746.  
  2747.  
  2748. /* Helper routine for make_symbol_completion_list.  */
  2749.  
  2750. int return_val_size, return_val_index;
  2751. char **return_val;
  2752.  
  2753. void
  2754. completion_list_add_symbol (symname)
  2755.      char *symname;
  2756. {
  2757.   if (return_val_index + 3 > return_val_size)
  2758.     return_val =
  2759.       (char **)xrealloc (return_val,
  2760.              (return_val_size *= 2) * sizeof (char *));
  2761.   
  2762.   return_val[return_val_index] =
  2763.     (char *)xmalloc (1 + strlen (symname));
  2764.   
  2765.   strcpy (return_val[return_val_index], symname);
  2766.   
  2767.   return_val[++return_val_index] = (char *)NULL;
  2768. }
  2769.  
  2770. /* Return a NULL terminated array of all symbols (regardless of class) which
  2771.    begin by matching TEXT.  If the answer is no symbols, then the return value
  2772.    is an array which contains only a NULL pointer.
  2773.  
  2774.    Problem: All of the symbols have to be copied because readline
  2775.    frees them.  I'm not going to worry about this; hopefully there
  2776.    won't be that many.  */
  2777.  
  2778. char **
  2779. make_symbol_completion_list (text)
  2780.   char *text;
  2781. {
  2782.   register struct symtab *s;
  2783.   register struct partial_symtab *ps;
  2784.   register struct block *b, *surrounding_static_block = 0;
  2785.   extern struct block *get_selected_block ();
  2786.   register int i, j;
  2787.   struct partial_symbol *psym;
  2788.  
  2789.   int text_len = strlen (text);
  2790.   return_val_size = 100;
  2791.   return_val_index = 0;
  2792.   return_val =
  2793.     (char **)xmalloc ((1 + return_val_size) *sizeof (char *));
  2794.   return_val[0] = (char *)NULL;
  2795.  
  2796.   /* Look through the partial symtabs for all symbols which begin
  2797.      by matching TEXT.  Add each one that you find to the list.  */
  2798.  
  2799.   for (ps = partial_symtab_list; ps; ps = ps->next)
  2800.     {
  2801.       /* If the psymtab's been read in we'll get it when we search
  2802.      through the blockvector.  */
  2803.       if (ps->readin) continue;
  2804.  
  2805.       for (psym = global_psymbols.list + ps->globals_offset;
  2806.        psym < (global_psymbols.list + ps->globals_offset
  2807.            + ps->n_global_syms);
  2808.        psym++)
  2809.     {
  2810.       QUIT;            /* If interrupted, then quit. */
  2811.       if ((strncmp (SYMBOL_NAME (psym), text, text_len) == 0))
  2812.         completion_list_add_symbol (SYMBOL_NAME (psym));
  2813.     }
  2814.       
  2815.       for (psym = static_psymbols.list + ps->statics_offset;
  2816.        psym < (static_psymbols.list + ps->statics_offset
  2817.            + ps->n_static_syms);
  2818.        psym++)
  2819.     {
  2820.       QUIT;
  2821.       if ((strncmp (SYMBOL_NAME (psym), text, text_len) == 0))
  2822.         completion_list_add_symbol (SYMBOL_NAME (psym));
  2823.     }
  2824.     }
  2825.  
  2826.   /* At this point scan through the misc function vector and add each
  2827.      symbol you find to the list.  Eventually we want to ignore
  2828.      anything that isn't a text symbol (everything else will be
  2829.      handled by the psymtab code above).  */
  2830.  
  2831.   for (i = 0; i < misc_function_count; i++)
  2832.     if (!strncmp (text, misc_function_vector[i].name, text_len))
  2833.       completion_list_add_symbol (misc_function_vector[i].name);
  2834.  
  2835.   /* Search upwards from currently selected frame (so that we can
  2836.      complete on local vars.  */
  2837.   for (b = get_selected_block (); b; b = BLOCK_SUPERBLOCK (b))
  2838.     {
  2839.       if (!BLOCK_SUPERBLOCK (b))
  2840.     surrounding_static_block = b; /* For elmin of dups */
  2841.       
  2842.       /* Also catch fields of types defined in this places which
  2843.      match our text string.  Only complete on types visible
  2844.      from current context.  */
  2845.       for (i = 0; i < BLOCK_NSYMS (b); i++)
  2846.     {
  2847.       register struct symbol *sym = BLOCK_SYM (b, i);
  2848.       
  2849.       if (!strncmp (SYMBOL_NAME (sym), text, text_len))
  2850.         completion_list_add_symbol (SYMBOL_NAME (sym));
  2851.  
  2852.       if (SYMBOL_CLASS (sym) == LOC_TYPEDEF)
  2853.         {
  2854.           struct type *t = SYMBOL_TYPE (sym);
  2855.           enum type_code c = TYPE_CODE (t);
  2856.  
  2857.           if (c == TYPE_CODE_UNION || c == TYPE_CODE_STRUCT)
  2858.         for (j = TYPE_N_BASECLASSES (t); j < TYPE_NFIELDS (t); j++)
  2859.           if (TYPE_FIELD_NAME (t, j) &&
  2860.               !strncmp (TYPE_FIELD_NAME (t, j), text, text_len))
  2861.             completion_list_add_symbol (TYPE_FIELD_NAME (t, j));
  2862.         }
  2863.     }
  2864.     }
  2865.  
  2866.   /* Go through the symtabs and check the externs and statics for
  2867.      symbols which match.  */
  2868.  
  2869.   for (s = symtab_list; s; s = s->next)
  2870.     {
  2871.       b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
  2872.       
  2873.       for (i = 0; i < BLOCK_NSYMS (b); i++)
  2874.     if (!strncmp (SYMBOL_NAME (BLOCK_SYM (b, i)), text, text_len))
  2875.       completion_list_add_symbol (SYMBOL_NAME (BLOCK_SYM (b, i)));
  2876.     }
  2877.  
  2878.   for (s = symtab_list; s; s = s->next)
  2879.     {
  2880.       b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
  2881.  
  2882.       /* Don't do this block twice.  */
  2883.       if (b == surrounding_static_block) continue;
  2884.       
  2885.       for (i = 0; i < BLOCK_NSYMS (b); i++)
  2886.     if (!strncmp (SYMBOL_NAME (BLOCK_SYM (b, i)), text, text_len))
  2887.       completion_list_add_symbol (SYMBOL_NAME (BLOCK_SYM (b, i)));
  2888.     }
  2889.  
  2890.   return (return_val);
  2891. }
  2892.  
  2893. void
  2894. _initialize_symtab ()
  2895. {
  2896.   add_info ("variables", variables_info,
  2897.         "All global and static variable names, or those matching REGEXP.");
  2898.   add_info ("functions", functions_info,
  2899.         "All function names, or those matching REGEXP.");
  2900.  
  2901.   /* FIXME:  This command has at least the following problems:
  2902.      1.  It prints builtin types (in a very strange and confusing fashion).
  2903.      2.  It doesn't print right, e.g. with
  2904.          typedef struct foo *FOO
  2905.      type_print prints "FOO" when we want to make it (in this situation)
  2906.      print "struct foo *".
  2907.      I also think "ptype" or "whatis" is more likely to be useful (but if
  2908.      there is much disagreement "info types" can be fixed).  */
  2909.   add_info ("types", types_info,
  2910.         "All types names, or those matching REGEXP.");
  2911.  
  2912. #if 0
  2913.   add_info ("methods", methods_info,
  2914.         "All method names, or those matching REGEXP::REGEXP.\n\
  2915. If the class qualifier is ommited, it is assumed to be the current scope.\n\
  2916. If the first REGEXP is ommited, then all methods matching the second REGEXP\n\
  2917. are listed.");
  2918. #endif
  2919.   add_info ("sources", sources_info,
  2920.         "Source files in the program.");
  2921.  
  2922.   add_com ("rbreak", no_class, rbreak_command,
  2923.         "Set a breakpoint for all functions matching REGEXP.");
  2924.  
  2925.   /* FIXME:  The code below assumes that the sizes of the basic data
  2926.      types are the same on the host and target machines!!!  */
  2927.  
  2928.   builtin_type_void = init_type (TYPE_CODE_VOID, 1, 0, "void");
  2929.  
  2930.   builtin_type_float = init_type (TYPE_CODE_FLT, sizeof (float), 0, "float");
  2931.   builtin_type_double = init_type (TYPE_CODE_FLT, sizeof (double), 0, "double");
  2932.  
  2933.   builtin_type_char = init_type (TYPE_CODE_INT, sizeof (char), 0, "char");
  2934.   builtin_type_short = init_type (TYPE_CODE_INT, sizeof (short), 0, "short");
  2935.   builtin_type_long = init_type (TYPE_CODE_INT, sizeof (long), 0, "long");
  2936.   builtin_type_int = init_type (TYPE_CODE_INT, sizeof (int), 0, "int");
  2937.  
  2938.   builtin_type_unsigned_char = init_type (TYPE_CODE_INT, sizeof (char), 1, "unsigned char");
  2939.   builtin_type_unsigned_short = init_type (TYPE_CODE_INT, sizeof (short), 1, "unsigned short");
  2940.   builtin_type_unsigned_long = init_type (TYPE_CODE_INT, sizeof (long), 1, "unsigned long");
  2941.   builtin_type_unsigned_int = init_type (TYPE_CODE_INT, sizeof (int), 1, "unsigned int");
  2942.  
  2943.   builtin_type_long_long =
  2944.     init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
  2945.            0, "long long");
  2946.   builtin_type_unsigned_long_long = 
  2947.     init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
  2948.            1, "unsigned long long");
  2949.  
  2950.   builtin_type_error = init_type (TYPE_CODE_ERROR, 0, 0, "<unknown type>");
  2951. }
  2952.  
  2953.